Phone Number ( 0 ) validation

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 &lt; 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 &lt; 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 &lt; 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;
	 }
	&lt;/script&gt;
&lt;/head&gt;	
&lt;body&gt;
	&lt;form name="frmSample" method="post" action="#" onSubmit="return ValidatePhoneNumber()"&gt;
		&lt;p&gt;Enter a phone number : 
			&lt;input type="text" name="txtPhone"&gt;
		&lt;/p&gt;
		&lt;p&gt; 
			&lt;input type="submit" name="Submit" value="Submit"&gt;
		&lt;/p&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/body&gt;

</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

Will probably need tweaking, but something like this?

function numCheck(phoneNum){
	var phoneReg = /^\\(?(\\d{3})\\)?[-. ]?(\\d{3})[-. ]?(\\d{4})$/;
	var zerosReg = /[1-9]/g;
	var phoneMatch = phoneNum.match(phoneReg);
	if (!phoneMatch || !zerosReg.test(phoneMatch)) { alert (phoneNum+" is an invalid number"); return; }
	else { alert (phoneNum+' is okay!!'); }
}

RLM

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.

I have to check only for all 0’s

Well for starters unless I’m missing something, if you look at the code

var zerosReg = /[1-9]/g; // i.e. are there any numbers throughout that fall in the range 1 to 9

and

!zerosReg.test(phoneMatch) // if not a match to numbers 1-9 i.e. all zeros

Should cover the zeros check? In other words check that the match has at least one number, rather than check for all zeros.

I’ll have a look at the rest of the function, but it would be easier if you could actually list maybe 5-10 examples of valid numbers in their entirety. It would save a bit of the leg work.:smiley:

RLM