Skip to content

Instantly share code, notes, and snippets.

@homaily
Last active June 30, 2024 05:39
Show Gist options
  • Save homaily/8672499 to your computer and use it in GitHub Desktop.
Save homaily/8672499 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.

@ahmed-alhelali
Copy link

So grateful!
thanks a lot, and you also @hasher313

@MohammadEdrees
Copy link

MohammadEdrees commented Jan 17, 2023

تسلم ايدك يا هندسة
^(009665|9665|+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$
ده شغال#C ممكن تضيفه عندك لو حابب
مثال بال #C

using System;

using System.Text.RegularExpressions;

public class Example

{

public static void Main()
{
    string pattern = @"^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$";

    string input = @"+966566846511";

    RegexOptions options = RegexOptions.Multiline;
    
    foreach (Match m in Regex.Matches(input, pattern, options))
    {
        Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
    }
}

}

@almgwary
Copy link

جزاك الله خيرا

@Saraa-39
Copy link

يعطيك العافية , تعرف كيف اطبقه باستخدام flutter ?

@almgwary
Copy link

almgwary commented May 14, 2023

يعطيك العافية , تعرف كيف اطبقه باستخدام flutter ?

bool matchesRegex(String input, String pattern) {
  RegExp regex = new RegExp(pattern);
  return regex.hasMatch(input);
}

@mohammedterfa
Copy link

جزاك الله خير

@Sal7one
Copy link

Sal7one commented Oct 1, 2023

Support for telephone numbers (((009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7))|((0096601|96601|\+96601|01)(1|2|3|4|6|7)))([0-9]{7})

@excalibur1987
Copy link

جزاك الله خيراً، لكن هل هناك خدمة ما للتأكد من أن الرقم بالحقيقة فعال؟

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