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 -
Code:<!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



Reply With Quote

Bookmarks