javascript 使用 js 和正则表达式检查文本框的无效字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12931617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:28:16  来源:igfitidea点击:

check a textbox for invalid characters using js and regular expressions

javascripthtmlregex

提问by Rafa? Saltarski

I am having a hard time figuring out how RegExp work.

我很难弄清楚 RegExp 是如何工作的。

I need to rewrite some ASP code into html and js, and I've hit an obstacle in this part:

我需要将一些 ASP 代码重写为 html 和 js,而我在这部分遇到了障碍:

<asp:RegularExpressionValidator runat="server" id="RegExpValidator" controltovalidate="FileName" Display="Dynamic" ValidationExpression="[^#%&*:<>?/{|}]+">

Now, what I do is create an input textbox which will run a js function whenever its content is changing.

现在,我要做的是创建一个输入文本框,它会在其内容发生变化时运行一个 js 函数。

<input type="text" id="fileNameTextBox" class="ms-input" size="35" maxlength="123" onchange="regexValidator(this);"/>

function regexValidator(control) {
            var val = $(control).val();
            if(val == undefined || val == '') {

                $(control).attr("class", "invalid");
            } 
            else { 
            // Regex stuff goes in here
            }
        }

Now, for the life of me I can't figure out how to construct the regular expression. The ValidationExpression field i assume checks for invalid characters though it doesn't seem to be a properly constructed regex, and I can't figure out how to write it into a proper one to use with js. Could someone help me out with this?

现在,对于我的生活,我无法弄清楚如何构建正则表达式。我假设 ValidationExpression 字段检查无效字符,尽管它似乎不是一个正确构造的正则表达式,而且我不知道如何将它写入一个正确的用于与 js 一起使用的字段。有人可以帮我解决这个问题吗?

回答by pogo

If you want the regex to check for invalid characters in the field, you can use this.

如果您希望正则表达式检查字段中的无效字符,您可以使用它。

^.*?(?=[\^#%&$\*:<>\?/\{\|\}]).*$This will give you a match if there is at least one invalid character.

^.*?(?=[\^#%&$\*:<>\?/\{\|\}]).*$如果至少有一个无效字符,这将为您提供匹配项。

回答by Martin Ender

You are almost there. Now you just need to make sure, that your string onlyconsists of valid characters. Do this by adding anchors for the beginning and end of string, thus ensuring that the repeated sequence covers the whole string:

你快到了。现在您只需要确保您的字符串包含有效字符。通过为字符串的开头和结尾添加锚点来做到这一点,从而确保重复的序列覆盖整个字符串:

ValidationExpression="^[^#%&*:<>?/{|}]+$"

EDIT:I just realised that you probably also want to know how to create a regular expression from a string. You can simply pass a string to a regex constructor:

编辑:我刚刚意识到您可能还想知道如何从字符串创建正则表达式。您可以简单地将字符串传递给正则表达式构造函数:

new RegExp(validationExpressionGoesHere);

回答by FixMaker

[^#%&*:<>?/{|}]+looks like a valid expression to me (although typically regular expressions are enclosed in forward-slashes). It's basically checking to see of the filename contains any of the illegal characters within the square brackets (apart from the caret ^which indicates negation).

[^#%&*:<>?/{|}]+对我来说似乎是一个有效的表达式(尽管通常正则表达式包含在正斜杠中)。它基本上是检查文件名是否包含方括号内的任何非法字符(除了^表示否定的插入符号)。

function regexValidator(control) {
        var val = $(control).val();
        if(val == undefined || val == '') {

            $(control).attr("class", "invalid");
        } 
        else if(val.match(/[^#%&*:<>?/{|}]+/)) {
            // Valid
        }
        else {
            // Invalid
        }
    }