Skip to content

Instantly share code, notes, and snippets.

@mghdmi
Created April 30, 2022 16:05
Show Gist options
  • Save mghdmi/a0cd4c86dd579455cd71d2ae23461660 to your computer and use it in GitHub Desktop.
Save mghdmi/a0cd4c86dd579455cd71d2ae23461660 to your computer and use it in GitHub Desktop.
Iranian phone number regex (include persian digits)
// 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
@AliTaee
Copy link

AliTaee commented Apr 30, 2022

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)
})

@mghdmi
Copy link
Author

mghdmi commented May 1, 2022

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