Skip to content Skip to sidebar Skip to footer

Javascript Regex Phone Validation

Can anyone suggest a js function to validate a phone number which has to start with 0 and be followed by any other digit? I am not concerned about the size, because I have maxlengt

Solution 1:

You want to use the .match function of string variables. eg.

var myString = $("#myPhoneNumebrField").val();
if (myString.match(/0[0-9]+/))
{
  //Valid stuff here
}
else
{
  //Invalid stuff here
}

As suggested by others, you probably want a minimum size in which case you can change the regex to be /0[0-9]{8,10}/ which will make the regex only match if the string is between 8 and 10 characters long (Inclusive).

Solution 2:

This regex should do: /^0[0-9]+$/

But you should also implement a minimum size.

Solution 3:

  1. You do not need a regex for this, instead write a function which iterates over the string.
  2. Try to do it yourself, you can start with Learning Regular Expressions. You will learn much more if you do not quote a ready made solution from SOF.

Post a Comment for "Javascript Regex Phone Validation"