Reputation: 585
Do anyone have similar experience? I would like to write a regex to validate the key-in mobile in a form field. Mobile number either contain 11 or 8 digits, if 11 digits entry, only start with number 1 is accept, this is for China mobile no. For 8 digits only number start with number 5,6 or 9, it is Hong Kong mobile no.
Below is the regex I wrote to validate the Hong Kong mobile no.
/^[5,6,9]{1}[0-9]{7}$/
Is it possible to have one regex to validate both mobile number entry?
Best regards,
Kelvin.
Upvotes: 2
Views: 5518
Reputation: 369424
Yes it's possible with |
(PATTERN_A|PATTERN_B
matches PATTERN_A
or PATTERN_B
):
/^1[0-9]{10}$|^[569][0-9]{7}$/
BTW, remove ,
from character class. Otherwise, not only 5, 6, or 9, but also ,
is also matched.
Upvotes: 4