2

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.

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.