Why does the '!' character break my jQuery function?

I'm learning about regular expressions using jQuery and the RegExp  object using its constructor function. I've been given a set of special characters that will be allowed in my search box and, aside from the letters and numbers, have been incrementally adding each special character and then testing my jQuery function (see below). When I add the '!' character, and enter a search string in my text box that contains the '!' character, for some reason, the function gets by passed all together, and based on the rules that I've read, (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-negated-character-set), I don't understand why. Can someone help me to understand what is happening?

<script type="text/javascript">

            function ValidateSearchTxt(source, args) {
                args.IsValid = true;
                var val = $('#txtSearchText').val();
                var selectAllClinics = $("#ddlClinics").get(0).selectedIndex;

                var strSrchTxt = new RegExp(/[a-zA-Z0-9.,/\\_-!]/); <--WHEN I ADDED THE '!' THE FUNCTION IS COMPLETELY BY PASSED. WHY DOES THIS HAPPEN?

                if (!strSrchTxt.test(val)){
                    args.IsValid = false                

                //for (var i = 0, i < val.length; i++){
                //    args.IsValid = false

                if (val == "Enter Name or Account #") {
                    val = "";

                if ((selectAllClinics > 0 && val != "") || val != "") {
                    if (!(isNaN(val))) {
                        if (val.length <= 3) {
                            alert("<%=GetDisplayString("text.donors.ePassportScheduledEventList.VldSearchTxt1")%>" + "\n" + 
                            "<%=GetDisplayString("text.donors.ePassportScheduledEventList.VldSearchTxt2")%>" + "\n" + 
                            "<%=GetDisplayString("text.donors.ePassportScheduledEventList.VldSearchTxt3")%>" + "\n" + 
                            args.IsValid = false;
                else if (selectAllClinics == 0 && val == "") {
                    args.IsValid = false;


Open in new window

Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?
käµfm³d 👽Commented:
Did you really mean "any character between underscore and exclamation point?" Because when you put a hyphen between two characters, you get a character range. Hyphen either needs to be either escaped, the first character, or the last character unless you really want a range.
Michael SterlingWeb Applications DeveloperAuthor Commented:
I do notice that if I take the '!' out of the RegExp pattern, then the function,...functions properly, when I search using text that has the '!' character in it. Why is this?
Michael SterlingWeb Applications DeveloperAuthor Commented:
Understood. You are correct, I do not want a range so I understand your explanation perfectly.Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.