Created
June 26, 2018 10:05
-
-
Save ErFUN-KH/581419e080d92410507950187c2875b2 to your computer and use it in GitHub Desktop.
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
/// convert persian number to en | |
function persianToEnDigit(data) { | |
return data.replace(/[\u06F0-\u06F9]+/g, function (digit) { | |
var ret = ''; | |
for (var i = 0, len = digit.length; i < len; i++) { | |
ret += String.fromCharCode(digit.charCodeAt(i) - 1728); | |
} | |
return ret; | |
}); | |
} | |
/// convert arabic number to en | |
function arabicToEnDigit(data) { | |
return data.replace(/[\u0660-\u0669]+/g, function (digit) { | |
var ret = ''; | |
for (var i = 0, len = digit.length; i < len; i++) { | |
ret += String.fromCharCode(digit.charCodeAt(i) - 1584); | |
} | |
return ret; | |
}); | |
} | |
/// convert arabic char to persian | |
function arabicToPersiaChar(data) { | |
data = data.replace(/[ي]+/g, 'ی'); | |
data = data.replace(/[Ùƒ]+/g, 'Ú©'); | |
return data; | |
} | |
/// automatic clean number and character on input | |
$('body').on('input', 'input', function () { | |
var self = $(this); | |
var caretPos = self.context.selectionStart; | |
if(!self.hasClass('no-char-change')){ | |
self.val( | |
arabicToEnDigit( | |
persianToEnDigit( | |
arabicToPersiaChar( | |
self.val() | |
) | |
) | |
) | |
); | |
self.context.setSelectionRange(caretPos, caretPos); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment