Created
April 30, 2022 16:05
-
-
Save mghdmi/a0cd4c86dd579455cd71d2ae23461660 to your computer and use it in GitHub Desktop.
Iranian phone number regex (include persian digits)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Validate numbers that starts with '+98' or '09' and 9 digits after. | |
// It also works with persian numbers. | |
const regex = /^((\+98|0|\+۹۸|۰)(9|۹)[0-9-۰-۹]{9})$/; | |
console.log(regex.test("+989121234567")); // true | |
console.log(regex.test("۰۹۱۲۱۲۳۴۵۶۷")); // true | |
console.log(regex.test("۰۹12۱۲34۵۶7")); // true | |
console.log(regex.test("+۹۸۹۱۲1234567")); // true | |
console.log(regex.test("+98912123456789")); // false | |
console.log(regex.test("0989121234567")); // false | |
It's great 🤩.
Also here's some unit tests in Jest:
const PersianMobileRegex = /^((\+98|0|\+۹۸|۰)(9|۹)[0-9-۰-۹]{9})$/ test.each([ ['۰۹۱۲۱۲۳۴۵۶۷', true], ['۰۹12۱۲34۵۶7', true], ['+989121234567', true], ['+۹۸۹۱۲1234567', true], ['0989121234567', false], ['+98912123456789', false], ])('Phone number %s is valid %o', (input, result) => { expect(PersianMobileRegex.test(input)).toBe(result) })
Thanks ali! 🤟❤
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's great 🤩.
Also here's some unit tests in Jest: