homepage Welcome to WebmasterWorld Guest from 54.211.68.132
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Validating a telephone number
Jamier101



 
Msg#: 4455940 posted 1:50 pm on May 21, 2012 (gmt 0)

Hi,

I've written a small piece of script to check if a field is blank and if it is to then display a message. I was wondering how I would go about checking that the data is greater than 9 digits and only numbers?

var Telephone=document.forms["ecollection"]["Telephone"].value;
if (Telephone==null || Telephone=="")
{
alert("Please enter your telephone number.");
return false;
}


I thought the following section of code would work but it doesn't.

var Telephone=document.forms["ecollection"]["Telephone"].value;
if (Telephone == <9)
{
alert("Please enter a valid telephone number");
return false;
}

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4455940 posted 3:07 pm on May 21, 2012 (gmt 0)

If you're trying to check the length of Telephone, then you need to test against the length property. Also, to test for greater than or equal to, the operator is >=


var Telephone = document.forms["ecollection"]["Telephone"].value;
if (Telephone.length >= 9)
{
alert("Please enter a valid telephone number");
return false;
}

This still has room for improvement. For example, if someone enters 9 spaces, this will incorrectly pass validation. So you should trim off any leading or trailing whitespace before testing.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4455940 posted 4:27 pm on May 21, 2012 (gmt 0)

Another approach is to **strip everything not a number** into a test variable, then test that. This allows the user to use their own delimiters (dashes, dots, none . . . ) It also makes it easier to compare lengths against the selected country so it can be international.

rainborick

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4455940 posted 4:32 pm on May 21, 2012 (gmt 0)

Try this function:

function checkPhone(userString) {
var valid = true;
var reason = '';
var minLength = 9;
var maxLength = 9;
// Remove non-digits
var phoneNumber = userString.replace(/([^\d])/g, '');
if (phoneNumber.length > maxLength) {
valid = false;
reason += 'Phone Number is too long.';
}
if (phoneNumber.length < minLength) {
valid = false;
reason += 'Phone Number is too short.';
}
if (phoneNumber != userString) {
valid = false;
reason += 'Invalid characters entered. Numbers only, please!';
}
if (!valid) { alert(reason); }
return valid;
} // end checkPhone()

Call checkPhone() with the number entered. It removes non-digits from the entry string, then checks the length.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4455940 posted 9:15 am on May 25, 2012 (gmt 0)

Is this for US and Canada only?

In that case, nine is the correct length.

If you're looking at other countries, the length can be anywhere from four to eleven (perhaps twelve) digits.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved