如何在7个字母/字母表/数字和新行中的新字母后的键向上插入分隔符-Jquery

How to insert delimitter on key up after 7 letters/alphabet/numbers and new letters in new line- Jquery

本文关键字:新字母 -Jquery 分隔符 插入 7个 数字 字母表 新行中      更新时间:2023-09-26

如何在keyup事件中在7个字母后放置逗号,下面是我的代码,我可以在7个字符后获得逗号,但当它转到下一行时,它无法正常工作。

 <asp:TextBox ID ="txtbillno" runat="server" onkeyup="InsertComma();" TextMode="MultiLine"></asp:TextBox>

 function InsertComma() {
      var txtObj = document.getElementById('<%=txtbillno.ClientID %>');
      var txtVal = replaceAll(txtObj.value, ',', '');
      if (txtObj.value != "") {
           var newVal = "";
           for (var i = 0; i < txtVal.length; i++) {
                newVal = newVal + txtVal.substring(i, i + 1);
                if ((i + 1) % 7 == 0 && i != 0) {
                    newVal = newVal + "," + "'n";
                }
           }
            txtObj.value = newVal;
      }
 }
 function replaceAll(txt, replace, with_this) {
      return txt.replace(new RegExp(replace, 'g'), with_this);
 }

InsertComma函数中,您正确地删除了逗号:

var txtVal = replaceAll(txtObj.value, ',', '');

但是txtVal仍然包含新行字符(''n),所以我也添加了一行来删除这些字符:

txtVal = replaceAll(txtVal, ''n', '');

完整的InsertComma方法是:

function InsertComma() {
  var txtObj = document.getElementById('txtbillno');
  // Remove commas
  var txtVal = replaceAll(txtObj.value, ',', '');
  // Remove new line characters
  txtVal = replaceAll(txtVal, ''n', '');
  if (txtObj.value != "") {
       var newVal = "";
       // Append commas
       for (var i = 0; i < txtVal.length; i++) {
            newVal = newVal + txtVal.substring(i, i + 1);
            if ((i + 1) % 7 == 0 && i != 0) {
                newVal = newVal + "," + "'n";
            }
       }
        txtObj.value = newVal;
  }
 }

在这里可以找到一个工作示例