Hi,

I want to validate phone number in my project. I include some validations. Furthermore i want to check whether it exactly contains ONLY 10 digits.nothing more and nothing less.How should i include that?

if (document.form1.phone_number.value == '') 
    {
        alert('Please fill in phone number!');
        return false;
    }
	if(!document.form1.phone_number.value.match(/^[0-9]+$/)){
	   alert('Please enter only numbers');
	   return false;

Recommended Answers

All 7 Replies

function validNumber(str)
{
	var tomatch= /^[0-9]{10}$/		
	if(tomatch.test(str))    
		return true;	
	else
		return false;    
}

Or, to keep the line count down :

function validNumber(str) { return /^[0-9]{10}$/.test(str); }

That's Vibhadevit's code reduced to its simplest form.

And the calling function would be :

function xxx(){
  ...
  if(!validNumber(document.form1.phone_number.value))
    alert('Please enter a valid phone number!');
    return false;
  }
  ...
}

or do the test in the calling function - without a specialist function:

function xxx(){
  ...
  if (!/^[0-9]{10}$/.test(document.form1.phone_number.value)){
    alert('Please enter a valid phone number!');
    return false;
  }
  ...
}

Airshow

Member Avatar for stbuchok

heshanm, if this is a public site, limiting people to how they can format the phone number is not a good idea.

Personally when I fill out my phone number I always use the format (000) 000-0000.

If you want only numbers and will be formatting it how you like when you display, you should strip all characters people use in a phone number and then see if there are only numeric values left with a length of 10 or 11 (1800 numbers or a leading 1).

This will allow your users to fill it in how they feel most comfortable and still allows you to do your validation. Always try to figure out what the best and easiest is for the user.

Just my 2 cents.

P.S. this will not validate for international phone numbers (the web is global).

Good point.

Thanks everyone.
@ stbuchoc; This is a kind of MIS for a bank and it is run within the bank itself. Therefore no issue with phone numbers.

I have confused a bit to find where to place the code. This is my whole validation script.

<script language = "Javascript">
  
function Validate()
{
    function validateEmail(f){
	if(isEmail(f.email.value)==false)
	{
	alert('Please enter a valid email address');
	f.email.select();
	return false;
	}
	return true;
	}
	function isEmail(string){
	if(string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z-0-9]+((\.|-)[A-Za-z-0-9]+)*\.[A-Za-z-0-9]+$/)!=-1)
	return true;
	else
	return false;
	}
	
	if (document.form1.first_name.value == '') 
    {
        alert('Please fill in your first name!');
        return false;
	}	
	if(!document.form1.first_name.value.match(/^[a-zA-Z]+$/)){
	    alert('Please enter only string values!');
		return false;
	}
	if (document.form1.last_name.value == '') 
    {
        alert('Please fill in your last name!');
        return false;
    }
	if(!document.form1.last_name.value.match(/^[a-zA-Z]+$/)){
	    alert('Please enter only string values!');
		return false;
	}	
    if (isEmail(document.form1.email.value) == '') 
    {
       alert('Please enter a valid email address!');
       return false;
    }
	if (document.form1.phone_number.value == '') 
    {
        alert('Please fill in phone number!');
        return false;
    }
	if(!document.form1.phone_number.value.match(/^[0-9]+$/)){
	   alert('Please enter only numbers');
	   return false;
	}   
	    
    if (document.form1.user_type.value == '') 
    {
        alert('Please fill in your user type!');
        return false;
    }
    if (document.form1.username.value == '') 
    {
        alert('Please fill in your desired username!');
        return false;
    }
    if (document.form1.password.value == '') 
    {
       alert('Please fill in your desired password!');
      return false;
    }
    if (document.form1.confirm_password.value == '') 
    {
       alert('Please fill in your password again for confirmation!');
      return false;
    }
    if (document.form1.password.value != 
    document.form1.confirm_password.value) 
    {
        alert("The two passwords are not identical! "+
        "Please enter the same password again for confirmation");
        return false;
    }
    
    return true;
}
</script>
<script language = "Javascript">
function isValidNumber(str)
{
	var tomatch= /^[0-9]{10}$/		
	if(tomatch.test(str))    
		return true;	
	else
		return false;    
}
function Validate()
{
    function validateEmail(f){
	if(isEmail(f.email.value)==false)
	{
	alert('Please enter a valid email address');
	f.email.select();
	return false;
	}
	return true;
	}
	function isEmail(string){
	if(string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z-0-9]+((\.|-)[A-Za-z-0-9]+)*\.[A-Za-z-0-9]+$/)!=-1)
	return true;
	else
	return false;
	}
	
	if (document.form1.first_name.value == '') 
    {
        alert('Please fill in your first name!');
        return false;
	}	
	if(!document.form1.first_name.value.match(/^[a-zA-Z]+$/)){
	    alert('Please enter only string values!');
		return false;
	}
	if (document.form1.last_name.value == '') 
    {
        alert('Please fill in your last name!');
        return false;
    }
	if(!document.form1.last_name.value.match(/^[a-zA-Z]+$/)){
	    alert('Please enter only string values!');
		return false;
	}	
    if (isEmail(document.form1.email.value) == '') 
    {
       alert('Please enter a valid email address!');
       return false;
    }
	if (document.form1.phone_number.value == '') 
    {
        alert('Please fill in phone number!');
        return false;
    }
	else
	{
		if(!isValidNumber(document.form1.phone_number.value))
		{
	   		alert('Please enter only numbers');
	   		return false;
		}   
	}	    
    if (document.form1.user_type.value == '') 
    {
        alert('Please fill in your user type!');
        return false;
    }
    if (document.form1.username.value == '') 
    {
        alert('Please fill in your desired username!');
        return false;
    }
    if (document.form1.password.value == '') 
    {
       alert('Please fill in your desired password!');
      return false;
    }
    if (document.form1.confirm_password.value == '') 
    {
       alert('Please fill in your password again for confirmation!');
      return false;
    }
    if (document.form1.password.value != 
    document.form1.confirm_password.value) 
    {
        alert("The two passwords are not identical! "+
        "Please enter the same password again for confirmation");
        return false;
    }
    
    return true;
}
</script>

@vibhadevit; Thanks a lot...

Thanks everyone who help me out regarding this problem...:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.