Last active
December 19, 2017 20:57
-
-
Save qti3e/293b60ba3b6ec74c18a6a75266199caa to your computer and use it in GitHub Desktop.
Validate and normalize Iran phone numbers, supports for converting Persian digits
This file contains 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!