Sachin
Sachin

Reputation: 231

Regex to validate phone number pattern

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?

Upvotes: 1

Views: 813

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336078

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

Upvotes: 3

Related Questions