check a textbox for invalid characters using js and regular expressions

html, javascript, regex

Solution

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.

Problem

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?

Original source