在波斯语(波斯语)中打字时分成数千

时间:2018-05-13 19:20:32

标签: javascript jquery input

我正在使用此代码段在用户输入时分隔数千个输入字段。它可以正确使用英文字母但不响应波斯语(波斯语)。有没有办法让它兼容?

$('input.number').keyup(function(event) {

  // skip for arrow keys
  if (event.which >= 37 && event.which <= 40) return;

  // format number
  $(this).val(function(index, value) {
    return value
      .replace(/\D/g, "")
      .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="form-control number" type="text" value="0" name="subjectvalue" id="txt_subjectvalue">

3 个答案:

答案 0 :(得分:0)

您可以使用Cleave.js之类的内容来格式化您的输入。

<强>更新

它有很多库和插件。即使你可以用纯粹的js来做,这里是an answer that can help you

正如Mood在他的回答中提到的那样,您可以使用replace()方法执行此操作,如下所示:

var fnf = document.getElementById("formattedNumberField");
fnf.addEventListener('keyup', function(evt){
    var n = parseInt(this.value.replace(/\D/g,''),10);
    fnf.value = n.toLocaleString();
}, false);

答案 1 :(得分:0)

此代码将获取input元素当前具有的任何值,删除所有,.字符,并将 String 转换为数字使用接受语言参数的toLocaleString()

&#13;
&#13;
// listens to an "input" event on the <input> element
document.querySelectorAll('input')[0].addEventListener('input', onInput_Arabic);
document.querySelectorAll('input')[1].addEventListener('input', onInput_normal);

function onInput_normal(){
   // convert into a Number & cleanup the input's value 
   var v = +(this.value.replace(/[,|\.|\D]/g,''));
   // replace the input's value with the formatted wanted value
   this.value =  this.value ? v.toLocaleString() : '';
}

function onInput_Arabic(){
   // convert into a Number & cleanup the input's value 
   // var v = +(this.value.replace(/[,|\.|\D]/g,''));
   var v = transformNumbers.toEnglish( this.value );
   // replace the input's value with the formatted wanted value
   this.value =  this.value ? (+v).toLocaleString('ar-EG') : '';
}

// transforms Numerals from one language system to another
var transformNumbers = (function(){
    var numerals = {
        persian : ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"],
        arabic  : ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]
    };

    function fromEnglish(str, lang){
        var i, len = str.length, result = "";

        for( i = 0; i < len; i++ )
            result += numerals[lang][str[i]]; 

        return result;
    }

    return {
        toEnglish : function(str){
            var num, i, len = str.length, result = "";

            for( i = 0; i < len; i++ ){
                num = numerals["persian"].indexOf(str[i]);
                num = num != -1 ? num : numerals["arabic"].indexOf(str[i]);
                if( num == -1 ) continue // num = str[i];
                result += num; 
            }
              
            return result;
        },

        toPersian : function(str, lang){
            return fromEnglish(str, "persian");
        },

        toArabic : function(str){
            return fromEnglish(str, "arabic");
        }
    }
})();
&#13;
input{ width:90%; display:block; padding:5px; margin:1em 0; }
&#13;
<input placeholder='Type Number in Eastern-Arabic (٢٤٦٦٩٢٤٦٦٩)'>

<input placeholder='Type Number in common language (123456) '>
&#13;
&#13;
&#13;

检查我的other answer,了解如何将数字字符串转换为&#34;数千&#34;已分隔的字符串,并且仅允许输入中的数字,请参阅this answer

答案 2 :(得分:0)

此代码适用于伊朗人。此代码在波斯化数字后插入逗号。

function insertrialcamma( n ) {
        var m = "";
        for ( var i = 0; i < n.length; i++ ) {
            var c = n.substr( n.length - i - 1, 1 );
            if ( i % 3 == 0 & i > 0 ) {
                m = c + ',' + m;
            } else {
                m = c + m;
            }
        }
        return m;
    }
    function toFarsiNumber( n ) {
        var o = "";
        n = n.toString();
        const farsiDigits = [ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ];
        const englishDigits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
        for ( var i = 0; i < n.length; i++ ) {
            for ( var j = 0; j < englishDigits.length; j++ ) {
                if ( n.substr( i, 1 ) == englishDigits[ j ] ) {
                    o = o + farsiDigits[ j ];
                }
            }
        }
        return o;
    }
    var n = insertrialcamma(toFarsiNumber( '1222345' ));
    alert( n )