سؤال

How to write regular expression for following conditions:

  1. Should have only numbers
  2. Must be 8 digits long
  3. Must start with 8 or 9 or 6

So for I can do only for first two conditions. I am not sure how to do the third conditions My code is

 if (!preg_match('/^[0-9]{8}$/', $number))
هل كانت مفيدة؟

المحلول

You're almost there. Simply remove the first number from the character class and validate it at the start of your pattern...

/^(8|9|6)\d{7}$/

FYI - \d is the escape sequence for digits. I suppose you could also use this

/^[896]\d{7}$/

as it means about the same thing when you're only watching for a single character at the start.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top