1

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 maxlength="10" in my html code. I now use a simple js function in order to restrict letters and other symbols from the phone nr. Any help is welcome!

JMax
  • 26,109
  • 12
  • 69
  • 88
  • So, 012 would be a valid number? `maxlength` isn't `exactlength`! – Widor Oct 05 '11 at 12:58
  • if you want some form validation, you'd probably do it client side (i'll remove your other tags). Some jquery plugins will do the job very well: http://docs.jquery.com/Plugins/validation – JMax Oct 05 '11 at 13:01

3 Answers3

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).

Matt Fellows
  • 6,512
  • 4
  • 35
  • 57
0

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

But you should also implement a minimum size.

Dennis
  • 14,264
  • 2
  • 48
  • 57
0
  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.
Community
  • 1
  • 1
rocksportrocker
  • 7,251
  • 2
  • 31
  • 48