Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

validate phone number with js

javascript validate

  • Please log in to reply
5 replies to this topic

#1 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 31 October 2012 - 01:56 PM

Hi i need to validate my code with the js validate but i cant figure out how to make it check that the phone number is in numbers and that it only contains 10 digits i have tryed soooo many things any suggestions pls i wil post my original function

function validate_form ()

{

 valid = true;

	    if ( document.input.name.value == "" )

	    {

			    alert ( "Please enter your name" );

			    valid = false;

	    }

	

	if ( document.input.address.value == "" )

	    {

			    alert ( "Please enter your address address" );

			    valid = false;

	    }

	if (document.input.hphone.value.length != 10)

  {   

	alert("Invalid home phone number");

  }

	if ( document.input.town.value == "" )

	    {

			    alert ( "Please enter your Suburb or town" );

			    valid = false;

	    }

  	if ( document.input.postcode.value == "" )

	    {

			    alert ( "Please enter your postocde" );

			    valid = false;

	    }

	

	if ( document.input.hphone.value == "" )

	    {

			    alert ( "Please enter your home phone number" );

			    valid = false;	

  }  

	if ( document.input.wphone.value == "" )

	    {

			    alert ( "Please enter your work phone number" );

			    valid = false;

	    }

	      if ( document.input.fax.value == "" )

	    {

			    alert ( "Please enter your fax" );

			    valid = false;

	    }

	   

	      if ( document.input.daydropdown.value == "" )

	    {

			    alert ( "Please enter day for delivery" );

			    valid = false;

	    }

  

	if ( document.input.monthdropdown.value == "" )

	    {

			    alert ( "Please enter month for delivery" );

			    valid = false;

	    }

	      if ( document.input.yeardropdown.value == "" )

	    {

			    alert ( "Please enter year of delivery" );

			    valid = false;

	    }  

 

	    {

   var x=document.forms["input"]["email"].value;

   var atpos=x.indexOf("@");

   var dotpos=x.lastIndexOf(".");

   if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)

     {

     alert("Not a valid e-mail address");

     return false;

     } 

     

  return valid;

  }

  }


  • 0

#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 31 October 2012 - 02:01 PM

Easiest way would be in regex.
And since phone number is use everywhere, you don't even have to write it take a look at http://regexlib.com/...?k=phone number you will find the one you like
  • 0

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#3 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 31 October 2012 - 02:31 PM

hi thanks for your reply so
(^1300\d{6}$)|(^1800|1900|1902\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\d{4}$)|(^04\d{2,3}\d{6}$) is the code regex for australia but how do i write that into my code ?
  • 0

#4 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 31 October 2012 - 03:00 PM

i have found this example that works but im still wanting to learn how to write it with the expressions
var y = document.getElementById('hphone').value;
	   if(isNaN(y)||y.indexOf(" ")!=-1)
	   {
		  alert("Phone number must be in numbers.");
		  document.getElementById('hphone').focus();
		  return false;
	   }
	   if (y.length>10 || y.length<10)
	   {
		    alert("Phone number should be 10 digit");
		    document.getElementById('mobile_number').focus();
		    return false;
	   }

  • 0

#5 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 31 October 2012 - 07:40 PM

Here a small tutorial on the test function of javascript http://www.w3schools..._obj_regexp.asp
if "y" is the phone number and you want the regex for australia try something like that

/(^1300\d{6}$)|(^1800|1900|1902\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\d{4}$)|(^04\d{2,3}\d{6}$)/gi.test(y)
it will return a boolean telling you if the validation passed or not
  • 0

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 31 October 2012 - 08:56 PM

That Regex works :D
Do you need just to validate or to split the phone parts too?

In addition, you should mind that RegEx is numbers only, and will not allow spaces, dashes, or any other non-numeric character a regular user could put on it as a separator.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download