PDA

View Full Version : Phone Number ( 0 ) validation



dchopda
02-09-2010, 04:24 AM
Hi,

I am working on a javascript code for validating phone numbers against 0's.

The scenario is that,

1 ) If the phone number contains all 0's in it, an error message is displayed.
Eg. 0, 0000, 000-000-0000, 000,0000000000 etc..

2 ) Phone number should only take digits [0-9] and some delimiters ()-,.+ "
All other alphabets and special characters are not allowed.
Eg. 01, 102-125-0214, etc.. ( allowed )
sjjlkjkj, xllfs -09-49- ( not allowed )

3 ) The code I have written is as below -


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validation Test</title>
<script type="text/javascript" language="javascript">

var ValidChars = "0123456789";
var phoneNumberDelimiters = "()-,.+ "; // Special characters that can be included in a phone number
var IsNumber=true;
var Char;

function trim(strPhone)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not a whitespace, append to returnString.
for (i = 0; i < strPhone.length; i++)
{
// Check that current character isn't whitespace.
var c = strPhone.charAt(i);
if (c != " ") returnString += c;
}
return returnString;
}

function stripCharsInBag(strPhone, phoneNumberDelimiters){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in phoneNumberDelimiters, append to returnString.
for (i = 0; i < strPhone.length; i++)
{
// Check that current character isn't whitespace.
var c = strPhone.charAt(i);
if (phoneNumberDelimiters.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function IsNumeric(strPhone){
for (i = 0; i < strPhone.length && IsNumber == true; i++)
{
Char = strPhone.charAt(i);
// Checking for all 0's in the string - Valid for upto 16 0's only...........
/*if (strPhone == "0" || strPhone == "00" || strPhone == "000" || strPhone == "0000" || strPhone == "00000" || strPhone == "000000" || strPhone == "0000000" || strPhone == "00000000" || strPhone == "000000000" || strPhone == "0000000000" || strPhone == "00000000000" || strPhone == "000000000000" || strPhone == "0000000000000" || strPhone == "00000000000000" || strPhone == "000000000000000" || strPhone == "0000000000000000") return false; */
if (strPhone.match(/^0+$/)) return false;

if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}

function checkInternationalPhone(strPhone){
strPhone=trim(strPhone);
strPhone=stripCharsInBag(strPhone, phoneNumberDelimiters);
return (IsNumeric(strPhone));
}

function ValidatePhoneNumber(){
var Phone=document.frmSample.txtPhone;

if (checkInternationalPhone(Phone.value)==false){
alert("Please Enter a Valid Phone Number");
Phone.value="";
Phone.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="frmSample" method="post" action="#" onSubmit="return ValidatePhoneNumber()">
<p>Enter a phone number :
<input type="text" name="txtPhone">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</body>
</html>


4 ) The problem is that, If I enter any invalid characters, it gives me error and sends me back to the phone number field. And then if I enter a valid number, it again shows the same error.
I am unable to get the desired result.

5 ) For first time, I have hard-coded the 0 string to max. 16 0's. But if some one adds more than 17 0's, my code fails. ( As shown in the above code )

6 ) Then, I use a regex pattern, but the issue remains same as mentioned in 4 ) above.



Can anyone help me in optimizing the code and making the code full-proof ?
It's urgent as tomorrow is the lat date for my project submission in university.


Regards,

dchopda

traq
02-09-2010, 05:51 AM
On your form, make textboxes for the phone number that look like so:



( |___| ) |___| - |____|

Then validate the input.

The first box must have exactly 3 numbers, at least one of which must not be a 0.
The next box must have 3 numbers, the next, 4 (get it?)

You could probably safely require the first number of the area code and phone number to be non-0 (I don't think there are any legitimate numbers that start with 0).

This could also be modified to your purposes (country codes, etc.), while taking out a lot of coding overhead. You may also consider server-side validation (javascript is a good way to be "helpful" to your visitors, but they can always turn it off and submit whatever they want if they just want to be difficult).

dchopda
02-09-2010, 06:02 AM
Thanks buddy,

I have done it for all types of phone numbers and the solution you have given to me is of US type validation. The above code supports all formats of phone number with the delimiters as mentioned.

I have to check only for all 0's and numbers other than valid phone numbers.

Valid - 01, 123-456-7890, etc..

Invalid - 0, 00, 000, 000-000-0000, 000jdjjhj, 221hfhgfhdfj11, etc...


If possible can you please go through the function IsNumeric(strPhone) in the code and let me know some way to complete the validation.


Thanks.

traq
02-09-2010, 03:39 PM
well, no, because (as I indicated) it's unnecessary and far too tedious. If this is for a school assignment (I didn't catch that at first), then I shouldn't be doing it for you anyway.

You can do what you want by using a regular expression to check for at least one non-0 number.