Skip to content

Instantly share code, notes, and snippets.

@qti3e
Last active December 19, 2017 20:57
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 qti3e/293b60ba3b6ec74c18a6a75266199caa to your computer and use it in GitHub Desktop.
Save qti3e/293b60ba3b6ec74c18a6a75266199caa to your computer and use it in GitHub Desktop.
Validate and normalize Iran phone numbers, supports for converting Persian digits
let persian = {'۰': 0, '۱': 1, '۲': 2, '۳': 3, '۴': 4, '۵': 5, '۶': 6, '۷': 7, '۸': 8 , '۹': 9}
function validateAndNormalizePhone(phone){
if(!phone || typeof phone !== 'string')
return false
phone = phone.replace(/ /g, '')
let tmp = ''
for(let i = 0;i < phone.length;i++){
let letter = phone[i]
tmp += persian[letter] !== undefined ? persian[letter] : letter
}
phone = tmp
phone = phone.replace(/^(\+98|098|0)/, '')
if(phone.match(/^9\d{9}$/)){
return '+98' + phone
}
return false
}
@AliSawari
Copy link

impressive!
I haven't tested it yet but seems working to me
I will run a few testes and comment the results 👍
Well Done BTW!

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