Welcome to WebmasterWorld Guest from 54.146.221.231

Forum Moderators: open

Message Too Old, No Replies

Validating a telephone number

     
1:50 pm on May 21, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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;
}
3:07 pm on May 21, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


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.
4:27 pm on May 21, 2012 (gmt 0)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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.
4:32 pm on May 21, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:904
votes: 5


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.
9:15 am on May 25, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.