strev
asked on
Newbie Phone Validation
>Taller Mike, you provided me with a great regular >expression function:
function validatePhone( phoneNumber )
{
var objRegExp = /^\d{3}\-\d{4}$/;
return objRegExp.test(phoneNumber );
}
This will validate 3 digits, then a dash, then 4 digits. If you want to make the dash option, you would change the expression like so:
var objRegExp = /^\d{3}\-?\d{4}$/;
Either way, when you submit the form, you would validate the phone number by sending it's value to this function.
>Now I have one additional expression I would like to add >to this. Should the user desire NOT to enter the number >once the form phone input has focus and simply tab out, >how should I write the entire expression?
Thanks again
function validatePhone( phoneNumber )
{
var objRegExp = /^\d{3}\-\d{4}$/;
return objRegExp.test(phoneNumber
}
This will validate 3 digits, then a dash, then 4 digits. If you want to make the dash option, you would change the expression like so:
var objRegExp = /^\d{3}\-?\d{4}$/;
Either way, when you submit the form, you would validate the phone number by sending it's value to this function.
>Now I have one additional expression I would like to add >to this. Should the user desire NOT to enter the number >once the form phone input has focus and simply tab out, >how should I write the entire expression?
Thanks again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, exactly what I needed...
Happy Holidays!
Happy Holidays!
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function checkInternationalPhone(st
s=stripCharsInBag(strPhone
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
var Phone=document.formname.Ph
if ((Phone.value==null)||(Pho
alert("Valid Phone Number with Area Code or Country Code");
}
else if (checkInternationalPhone(P
alert("Valid Phone Number with Area Code or Country Code");
}