Welcome to WebmasterWorld Guest from 54.226.189.112

Forum Moderators: open

Message Too Old, No Replies

Validating a telephone number

     

Jamier101

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

3:07 pm on May 21, 2012 (gmt 0)

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



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

4:27 pm on May 21, 2012 (gmt 0)

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



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

4:32 pm on May 21, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

9:15 am on May 25, 2012 (gmt 0)

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



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month