1

How can I construct regx to validate phone number? That is:

  1. First digit must be 04 or 050 , length range between 8-13
  2. First digit cannot be 43 or 44 , first digit must be 4 or 9 and length should be 8 digits

I have tried this pattern:

^[04,050]\\d{8,13} 

Can any body help me?

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
Sachin
  • 221
  • 2
  • 4
  • 12
  • 3
    One does not simply *try* to validate phone numbers. – HamZa Jun 27 '13 at 10:17
  • 1
    http://stackoverflow.com/questions/17023765/insert-phone-number-into-database/17023851#17023851 – Denis de Bernardy Jun 27 '13 at 10:20
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – Toto Jun 27 '13 at 11:56

1 Answers1

3

Let's break it down (hoping I understand correctly):

^               # Start of string
(?:             # Match one of the following:
 04\d{6,11}     # Either an 8-13 digit number starting with 04
|               # or
 050\d{5,10}    # an 8-13 digit number starting with 050
|               # or
 4[0-25-9]\d{6} # an 8 digit number starting with 4 but not 43 or 44
|               # or
 9\d{7}         # an 8 digit number starting with 9
)               # End of alternation
$               # End of string
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I noticed you did thus before and I let it slide, but this time I'm going to pull you up: `(?:` does **not** mean "either", although saying so helps make your comments into a sentence, this fact carries no technical weight and may be misleading to novices. – Bohemian Jun 27 '13 at 11:45
  • @Bohemian: I'm aware of the potential for misunderstanding. I just don't know what to write instead that's both correct and understandable. How do you like the new version? – Tim Pietzcker Jun 27 '13 at 11:52
  • I respect your regex skills greatly, however the comments are just wrong. For starters, you are starting a group to allow an OR, that's what the bracket does, then you (unnecessarily) make it non-capturing with `?:`, presumably for "performance" reasons (optimising early?), then you introduce some or'ed terms. I would comment it basically like that – Bohemian Jun 27 '13 at 12:00
  • Hi Tim Pietzcker , thanks. It worked for me :) This is what I expect .... ^(?:04\\d{6,11}|050\\d{5,10}) and ^(?:4[0-25-9]\\d{6}|9\\d{7}) . Thank you once again !! – Sachin Jun 27 '13 at 12:04
  • @Bohemian: Well, that was the point of my "Either..." - you just wrote yourself that the bracket "allows the ORs". As for `(?:...)` vs. `(...)`, that's just a habit. I never use a capturing group if I don't want to capture its contents. Not because of optimization, but to clearly convey its purpose. – Tim Pietzcker Jun 27 '13 at 12:10
  • 1
    I would have just said `(?: # start of (non capturing) group`. Now, a serious comment - why have the group at all? Isn't it not needed? Won't the OR work anyway because it's "bracketed" by start/end of input? – Bohemian Jun 27 '13 at 13:47
  • @Bohemian: The group is necessary; otherwise you'd have `(^foo) | (bar) | (baz$)`. – Tim Pietzcker Jun 27 '13 at 14:36