Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WatheqAlshowaiter/020fa14aa8e3ef6ffb4cfac8307c4397 to your computer and use it in GitHub Desktop.
Save WatheqAlshowaiter/020fa14aa8e3ef6ffb4cfac8307c4397 to your computer and use it in GitHub Desktop.
Regex to validate saudi mobile numbers

السلام عليكم ، هذا كود ريجيكس بسيط للتحقق من صحة أرقام الجوالات السعودية ، يقوم الريجيكس بالتحقق من مفتاح الدولة ، مفتاح شركة الإتصالات لضمان صحة النص المدخل .

Hello, this is a simple regex to validate saudi mobile numbers, the code will validate country code, telecome company code and make sure the tested sting is correct .

/^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/

Regex Breakdown - شرح الكود

/^

التأكد أن النص المدخل في بداية السطر

Start of Line

(009665|9665|\+9665|05|5)

التأكد أن الرقم المدخل يبدأ بمفتاح السعودية أو المفتاح المحلي لأرقام الجوالات

Validate that the contry code is for Saudi Arabia

(5|0|3|6|4|9|1|8|7)

التأكد من مفتاح شركة الإتصالات

Validate thar the telecome company prefix is correct

  • 0, 5, 3 : STC prefix
  • 6, 4 : Mobily prefix
  • 9, 8 : Zain prefix
  • 7 : MVNO prefix (Virgin and Lebara)
  • 1 : Bravo prefix
[0-9]{7}

التحقق من وجود 7 خانات بعد مفتاح الدولة ومفتاح شركة الإتصالات

Validate that the input contains 7 digits after the country code and the telecome prefix.

$/

نهاية السطر

End of Line

Usage Examples - أمثلة لاستخدام الكود

PHP:

preg_match('/^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/', '0505330609'); // return true
preg_match('/^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/', '0525330609'); // return false

Javascript

var regex = new RegExp(/^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/);
regex.test('0501234567'); // return true;
regex.test('0521234567'); // return false;

thanks to http://twitter.com/motazG, https://twitter.com/ModmenNet and https://twitter.com/engineer_fouad for their help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment