<script language = "Javascript" type="text/javascript">
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()-,. ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 11;
function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function trim(s)
{ 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 < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (c != " ") returnString += c;
}
return returnString;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
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>
[list][]^ this is the start of the number *? an optional non-zero digit
[][2-9]\d{2} 3-digits from 200 upwards * optional parenthesis for area code
[]-? optional divider
[][2-9]\d{2} 3-digits from 200 upwards
[]-? optional divider
[]\d{4} four more digits
[*]$ end of the number[/list]
When you figure out what formats are acceptable and which are not, we can craft together a regular expression for you that will make matching them a breeze.
If you want to accept phone numbers from anywhere at all then there are not very many restrictions you can apply to them as different countries use different formats. There are even a few valid phone numbers that consist of nothing but zeros.
The validation will take place only if all the digits in the phone number are “0”. Whether it is one time or more ( maximum 16 digits ).
It will also take care of the delimiters – “±(),. ”. These delimiters are common in any phone number.
Validation will only allow occurrences of the numbers [0-9] and the delimiters mentioned above, in the phone number. It will not allow occurrences of all 0’s and other special characters.
The validation code is applicable to all types for phone number standards (maximum 16 digits ).
The code is -
<!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 = "()-,.+ ";
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);
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 (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>
I need further help.
Right now I am using hardcore values for comparing all 0’s.
Can anyone provide me a regular expression for comparing only 0’s in a phone number.