0

我正在尝试使用 JQuery 将文本框中的特殊字符数限制为 3。特殊字符可以按任意顺序排列,可以彼此相邻,也可以散布在文本框的长度上。在过去的 24 小时里,我阅读了 stackoverflow 中几乎所有相关的帖子并尝试了这些建议。

我还没有提供合适的正则表达式。

到目前为止,这是我的 ASP.net mvc 表单底部的内容:

<script type="text/javascript">
    $(document).ready(function () {
        $('input[id$=textbox1]').bind('blur', function () {
            var match = /^[.,:;!?€¥£¢$-~#%&*()_]{4,50}$/g.test(this.value);
            if (match == true)
                alert("Please enter a maximum of 3 special characters.");
        });
    });
</script>

以下是我要定位的特殊字符列表:

~`!@#%^&*()-_:;?€¥£¢$-~{}[]<>/+|=

4

3 回答 3

1

你在哪里思考困难。只需计算元素在输入中的频率,并在计数超过 3 时发出警告。

这是一个

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Demo</title>
        <script src="jquery-1.8.3.min.js"></script>

        <script>
            var limitchars = '[~`!@#%\^&\*\(\)-_:;\?€¥£¢\${}\[]<>/+\|=]'


            $(function () {
                var  teststring = "~`!@#%\^&\*\(\)-_:;\?€¥£¢\${}\[]<>/+\|='-:;";
                var regex = /[~`'!@#%\^&\*\(\)-_:;\?€¥£¢\${}\[\]<>/+\|=]/g

                //var test1 = teststring.match(regex);
                //debugger;
                //alert(teststring.length === test1.length);

                $('input').keyup(function () {
                    var text = this.value;

                    var match = text.match(regex);
                    if (match.length > 3) {
                        alert('invalidI input');
                    }
                });
            });
        </script>
    </head>
    <body>
        <input type="text" />
    </body>
</html>

不要忘记转义正则表达式(http://www.regular-expressions.info/reference.html)。正则表达式末尾的 g 计算字符串中出现的次数(http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string)

于 2012-11-28T10:42:25.943 回答
0

您可以通过计算匹配的数量来找到错误字符的数量。

http://jsbin.com/alidus/1/上的工作示例

 $(document).ready(function () {
        $('#test').bind('blur', function () {       

          var charactersToExlude = "~`!@#%^&*()-_:;?€¥£¢$-~{}[]<>/+|=";
          var badCharacterRegex = "[" + charactersToExlude.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&") + "]";
          console.log(badCharacterRegex);
          var patt=new RegExp(badCharacterRegex, "g");

          var matches = this.value.match(patt);

          var numberOfBadCharacters = matches ? matches.length : 0;

          if (numberOfBadCharacters >= 3) {
            $("#result").text("Please enter a maximum of 3 special characters. (" + numberOfBadCharacters + " bad characters).");
          } else {
            $("#result").text(numberOfBadCharacters + " bad characters.");
          }

        });
    });

首先,我们转义任何对正则表达式有意义的坏字符。然后创建模式,匹配它,如果有匹配,计算匹配的数量。

于 2012-11-28T10:42:25.797 回答
0
$(document).ready(function () {
    $('#test').bind('blur', function () {       

          var charactersToExlude = "~`!@#%^&*()-_:;?€¥£¢$-~{}[]<>/+|=";
          var badCharacterRegex = "[" + charactersToExlude.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&") + "]";
          console.log(badCharacterRegex);
          var patt=new RegExp(badCharacterRegex, "g");

          var matches = this.value.match(patt);

          var numberOfBadCharacters = matches ? matches.length : 0;

          if (numberOfBadCharacters >= 3) {
            $("#result").text("Please enter a maximum of 3 special characters. (" + numberOfBadCharacters + " bad characters).");
          } else {
            $("#result").text(numberOfBadCharacters + " bad characters.");
          }

    });
});
于 2013-01-22T10:00:17.600 回答