Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

javascript - check a textbox for invalid characters using js and regular expressions

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

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

<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.

<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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...