Skip to content

Instantly share code, notes, and snippets.

@thepearl
Created June 13, 2021 14:10
Show Gist options
  • Save thepearl/4457f6c717bfb0f16491ed6d04bda08e to your computer and use it in GitHub Desktop.
Save thepearl/4457f6c717bfb0f16491ed6d04bda08e to your computer and use it in GitHub Desktop.
Regex to validate Tunisian mobile numbers
السلام عليكم ، هذا كود ريجيكس بسيط للتحقق من صحة أرقام الجوالات التونسية ، يقوم الريجيكس بالتحقق من مفتاح الدولة ، مفتاح شركة الإتصالات لضمان صحة النص المدخل .
Hello, this is a simple regex to validate tunisian mobile numbers, the code will validate country code, telecome company code and make sure the tested sting is correct .
"^(00216|216|\+216)(9|5|4|2)([0-9]{7})$"
Regex Breakdown - شرح الكود
-----
```
(00216|216|\+216)
```
التأكد أن الرقم المدخل يبدأ بمفتاح تونس أو المفتاح المحلي لأرقام الجوالات
Validate that the contry code is for Tunisian country
```
(9|5|4|2)
```
التأكد من مفتاح شركة الإتصالات
Validate thar the telecome company prefix is correct
* `9` : Tunisie Telecom prefix
* `5` : Orange prefix
* `4` : Elissa prefix
* `2` : Oreedoo prefix
```
[0-9]{7}
```
التحقق من وجود 7 خانات بعد مفتاح الدولة ومفتاح شركة الإتصالات
Validate that the input contains 7 digits after the country code and the telecome prefix.
Usage Examples - أمثلة لاستخدام الكود
-----
### PHP:
preg_match('/^(00216|216|\+216)(9|5|4|2)([0-9]{7})$/', '21650311362'); // return true
preg_match('/^(00216|216|\+216)(9|5|4|2)([0-9]{7})$/', '21612123123'); // return false
### Javascript
var regex = new RegExp(/^(00216|216|\+216)(9|5|4|2)([0-9]{7})$/);
regex.test('21650311362'); // return true;
regex.test('21612123123'); // return false;
### Swift 5.0
extension String
{
func validateSAPhoneNumber() -> Bool
{
let PATTERN = #"^(00216|216|\+216)(9|5|4|2)([0-9]{7})$"#
return self.range(of: PATTERN ,options: .regularExpression) != nil
}
}
Text(phoneString.validateSAPhoneNumber() ? "✅" : "❌")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment