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

时间:2015-12-08 09:46:20

标签: javascript jquery css asp.net jquery-ui

如何在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);
 }

1 个答案:

答案 0 :(得分:0)

在您的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;
  }

 }

可以找到一个工作示例here