-1

i write this code to convert arabic number to english number when the user start typing inside testinput its working fine but how i can enable it to accepts negative value it's prevent me to type (-) on number inputs

jQuery(document ).on( "keyup", ".number", function(){
 var yas =$(this).val();
yas = Number( yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function(d) { return   d.charCodeAt(0)-1632;}).replace(/[٠١٢٣٤٥٦٧٨٩]/g, function(d) {return d.charCodeAt(0) - 1776;}) );
  if (isNaN (yas))
  {yas= 0;}
 $(this).val(yas);

 });
maali
  • 133
  • 3
  • 8

1 Answers1

0
String.prototype.toEnglishDigits = function () {
    var persian = { '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9' };
    var arabic = { '٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4', '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9' };
    return this.replace(/[^0-9.]/g, function (w) {
        return persian[w] || arabic[w] || w;
    });
};

jQuery(document ).on( "keyup", ".number", function(){
    var yas =$(this).val();
    yas = Number( (yas || '').toEnglishDigits() );
    if (isNaN (yas))
        yas = 0;
    $(this).val(yas);
 });

String.prototype.toEnglishDigits = function () {
    var persian = { '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9' };
    var arabic = { '٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4', '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9' };
    return this.replace(/[^0-9.]/g, function (w) {
        return persian[w] || arabic[w] || w;
    });
};

console.log('-۱۲۳۴.۵۶۷'.toEnglishDigits());
MeTe-30
  • 2,512
  • 2
  • 21
  • 30
  • same problem it's prevent me to add (-) negative number its convert the value to 0 if i typed (-) inside textinput – maali Jan 29 '17 at 11:54
  • @maali My code is correct dude, check the snippet code. Maybe your isNaN function is wrong! – MeTe-30 Jan 29 '17 at 12:29