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();
                val.trim();
                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 #") {
                    $('#txtSearchText').val('');
                    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" + 
                            "<%=GetDisplayString("text.donors.ePassportScheduledEventList.VldSearchTxt4")%>");
                            args.IsValid = false;
                        }
                    }
                }
                else if (selectAllClinics == 0 && val == "") {
                    alert("<%=GetDisplayString("text.donors.ePassportScheduledEventList.VldSearchRequired")%>");
                    args.IsValid = false;

                }
            }
        </script>

Open in new window

LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael SterlingWeb Applications DeveloperAuthor Commented:
Understood. You are correct, I do not want a range so I understand your explanation perfectly.Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.