正则表达式:如何限制字母数字字段中的最大整数数

时间:2019-04-09 10:35:28

标签: javascript jquery

我的指示是,用户可以在字段中键入除以下字符以外的所有字符:

~`!@#$%^&*()_+={}[]|\;"',<>?

最大长度为60。

字符串中的整数不能超过10。

例如,如果用户粘贴到输入中:

INV-123-RT-123456789-TR-123

然后正则表达式应输出:

INV-123-RT-1234567-TR-

这是我的代码。我坚持从字符串末尾删除多余的整数。

 $('.isInvoiceNumber').on("input", function(e) {

  var pos = this.selectionStart;

  var str = $(this).val().replace(/[\~\`\!\@\#\$\%\^\&\*\(\)_\+\=\{\}\[\]\|\\\;\"\'\,\<\>\?]/g, '').replace(/\.{2,}/g, '.'); 

  var digits = str.replace(/[^0-9]/g,"").length;
  if ( digits>10 ) {
    // ??
  }

  var len = str.length;
  $(this).val(str);
  this.selectionEnd = pos - ( len-str.length );
});

-> 使用 Codepen 可以简化此操作: https://codepen.io/btn-ninja/pen/vJrXbX

谢谢!

4 个答案:

答案 0 :(得分:1)

只需在您的if条件中进行检查:

var str = str.substr(0, str.length-1);

它将从输入字符串中删除最后输入的字符。

答案 1 :(得分:1)

赞:

 $('.isInvoiceNumber').on("input", function(e) {

  var pos = this.selectionStart;

  var str = $(this).val().replace(/[\~\`\!\@\#\$\%\^\&\*\(\)_\+\=\{\}\[\]\|\\\;\"\'\,\<\>\?]/g, '').replace(/\.{2,}/g, '.'); 

  var digits = str.replace(/[^0-9]/g,"").length;
  if ( digits>10 ) {
        str = str.substring(0, 10);
  }

  var len = str.length;
  $(this).val(str);
  this.selectionEnd = pos - ( len-str.length );
});

答案 2 :(得分:1)

您的意思是输入验证?像这样吗?:

//use 'keydown' to prevent copy pasting invalid values
var inputLength = 0;
$('#invoice').on('keydown', function(){inputLength = $(this).val().length});
//check against criteria set upon input
$('#invoice').on('keyup', function(){
  if($(this).val().length>60 || $(this).val().match(/[0-9]/g).length>15 || $(this).val().match(/[^\w^0-9]/)){
    $(this).val($(this).val().slice(0,inputLength-$(this).val().length));
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="invoice"></input>

答案 3 :(得分:1)

尝试一下:

function limit( str ) {
  var patt = /[~`!@#\$%\^&\*()_\+={}\[\]\|\;\"\'\,\<\>\?]/g;
  var strWithoutSpecialChars = str.replace(patt,'') ;
  var count = 0, result = '', numberDigit = 0 ;
  for (var i = 0; i < strWithoutSpecialChars.length && count < 60; i++ ) {
    var ch = strWithoutSpecialChars[i] ;
    if ( /\d/.test(ch) ) {
      numberDigit++;
      if ( numberDigit < 15 ) { result += ch; count++ ; }
      else { continue ; }
    }
    else { result += ch; count++ ; }
  }
 return result ;
}

var longText = 'Miusov, 5699999999as a man man of breeding and 555deilcacy, could 98955not but feel some inwrd qualms, when he reached the Father Superiors with Ivan: he felt ashamed of havin lost his temper. He felt that he ought to have disdaimed that despicable wretch, Fyodor Pavlovitch, too much to have been upset by him in Father Zossimas cell, and so to have forgotten himself. "Teh monks were not to blame, in any case," he reflceted, on the steps. "And if theyre decent people here (and the Father Superior, I understand, is a nobleman) why not be friendly and courteous withthem? I wont argue, Ill fall in with everything, Ill win them by politness, and show them that Ive nothing to do with that Aesop, thta buffoon, that Pierrot, and have merely been takken in over this affair, just as they have.';

var result = limit(longText) ;
console.log('Length : ' + result.length ) ;
console.log( 'String : ' + result ) ;