1

Possible Duplicate:
A comprehensive regex for phone number validation

I am trying to come out for the right pattern to validate a phone number in Javascript. The num is in the following format: the phone num is 10 digits the phone num must start with 012,013,019,014, or 016. The process is very confusing for me .. can anyone help please? Highly appreciated in advance ..

Community
  • 1
  • 1
Alsaket
  • 175
  • 1
  • 11

5 Answers5

3

try:

 /^01[23469]\d{7}$/.test("your telephone number");
Ya Zhuang
  • 4,652
  • 31
  • 40
2

This pattern should work

^01[23469]\d{7}$
Tetaxa
  • 4,375
  • 1
  • 19
  • 25
1

Use a regex like this:

phone.match(/^01[23469]\d{7}/)

where phone is your phone number variable. The regex means it has to start (^) with 0, then 1, then any of 2,3,4,6,9 followed by 7 more digits.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
0

I think you want to use a regular expression here.

The example/tutorial here should steer you in the right direction.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • Thank youuu everyone here who helped me so much.. I really appreciate your very kind replies on my questions – Alsaket Dec 02 '11 at 20:05
0

How 'bout:

if (/^(?:012|013|019|014|016)\d{7}$/.test(str)) {
    // okay
}

That's a regular expression (MDC has a good page on them) which looks for an alternation (any of 012, 013, 019, 014, or 016) followed by exactly seven digits. The ^ at the start matches the start of the string, and the $ at the end matches the end, disallowing extraneous characters. You might consider trimming whitespace before doing this.

I used an alternation rather than ^01[23946] just for clarity and to allow for other prefixes being easily added.

Live test


Side note: I always try to discourage people from validating phone numbers. It just ends up irritating people when they need (need) to input something your rules haven't allowed for. Also, usually 10-digit phone numbers are written in groups, e.g. here in the UK, "01234 567 890" (sometimes written "01234 56 78 90") or "020 8123 4567" (sometimes written "0208 123 4567"). You'd have to strip out internal spaces before validating to allow people to write them in the various ways people do.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I really owe you so much , all of you. You helped me so much in stackoverflow to submit my final year project SUCCESSFULLY through answering my questions in (Android, Ajax, Javascript, Google Maps API3, and PHP).. I really thank you great people .. – Alsaket Dec 05 '11 at 13:36