Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a standard phone number validation script around?

Status
Not open for further replies.

JohnBeals

Programmer
Feb 26, 2002
49
US
I am looking for a standard phone number validation script. Mine is below, but it's a bare minimum. I'd like something a little more robust without having to reinvent the wheel.

If you have one you'd be willing to share, I'd appreciate it.

JB

function isPhoneNumber(inputVal)
{
inputStr = inputVal.toString()
for (var i = 0; i < inputStr.length; i++)
{
var oneChar = inputStr.charAt(i)
if (!(oneChar == &quot;(&quot; || oneChar == &quot;)&quot; || oneChar == &quot;-&quot; || (oneChar >= &quot;0&quot; && oneChar <= &quot;9&quot;)))
{
alert(&quot;Phone Number must contain numbers, parentheses and dashes only. Invalid Character was:&quot; + oneChar)
document.forms(0).txtPHONE.value = &quot;&quot;
document.forms(0).txtPHONE.focus()
return
}
}
return true
}
 
hi john,

I've written one as a regular expression:

function isPhoneNum(s) {
var pat = /^(\d?\s?[\(\-\.\s]?\s?\d{3}[\)\-\.\s]?)?\s?\d{3}\s?[\.\-\s]?\s?\d{4}$/;
return pat.test(s);
}



it accepts phone numbers like:

1 222 333 4444
or
1 (222) 333 - 4444
or
1.222.333.4444
or
(222)333-4444
or
222-333-4444
or
12223334444
etc...

the acceptable delimiters are &quot;(&quot;, &quot;)&quot;, &quot;-&quot;, &quot;.&quot;, and &quot; &quot;, and they're optional.

=========================================================
if (!succeed) try();
-jeff
 
jemminger - excellent script! Mind if I use it?

Tnaks in advance There's always a better way...
 
jemminger,

Thanks for the script. This script has two effects. One, it solves my script problem, and two, it introduced me to a programming method I was previously unaware of: Regular Expressions in Javascript :)

Learn sumpin new ever'day!

JB
 
jb,
my pleasure...glad you like it! as you can see above, regexes can often do twice as much in half the space (or better) =========================================================
if (!succeed) try();
-jeff
 
I have one a bit longer but it removes any NaN chars even ()- then reformats it to
(111)111-1111
so
%111*111+1111
would return
(111)111-1111
and !@1*d1b1?(1-+1+@^1}|1&e1n:1bombboy1*((
would return
(111)111-1111
if it can't find exactly ten numbers you get an alert window that says must be a 10 digit number


function phoneformat(fld){
var newnumber = &quot;&quot;;
phonenumber = fld.value;
for(place = 0; place <= fld.value.length; place++)
newnumber = newnumber + phonenumber.charAt(place).replace(/\D*/, '');
if (newnumber.length == 10){
fld.value = '(' + newnumber.substring(0,3) + ')' + newnumber.substring(3,6) + '-' + newnumber.substring(6,newnumber.length);}
else if (newnumber.length == 0){
fld.value = ''}
else{
fld.select(),
alert('Invalid Phone Number must be 10 digit number');}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top