0

Can you help me write two rules that check phoneNumber?
First rule: start with +7 or 8 then any ten numbers. Examples:

+77017223457 - valid
77017223457 -not valid
87017223457 - valid

Second rule: start with +7 or 8 then three numbers from set:
{700, 701, 702, 705, 707, 712, 713, 717, 718,721, 725, 726, 727, 777 } then any seven numbers.

Examples:

+77074446255 - valid
+77034446255 - not valid (no 703 in the set)

Thanks.

3 Answers 3

1
var phone = $("#phoneNumber").val()
var regex = /^((\+7)|8)(700|701|702|705|707|712|713|717|718,721|725|726|727|777)[0-9]{7}$/;
if(regex.test(phone))
   // Phone is valid
else
   // Phone is invalid
Sign up to request clarification or add additional context in comments.

Comments

1
(?:\+7|8)(?:70[0-2]|705|707|71[23]|71[78]|721|72[5-7]|777)[0-9]+

or, less compressed but more obvious:

(?:\+7|8)(?:700|701|702|705|707|712|713|717|718|721|725|726|727|777)[0-9]+

To make sure that this is not a partial match, use ^ and $

^(?:\+7|8)(?:700|etc|etc)[0-9]+$

1 Comment

you regexp match on '+7701722345'. But, there is nine numbers after +7 (must be 10)
1

A little bit more compact:

/^(?/\+7|8)7(?:0[01257]|1[2378]|2[1567]|77)\d{7}$/

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.