Why is my RegExp object allowing characters that are not listed as valid?

My RegExp object is allowing characters, that I think/feel it should not be. I have a function that looks like the one below. When I enter a string such as:

 .,dhgf45,/\_-!@#$%^&*()+:";'

based on my new knowledge (I'm still learning regular expression validation syntax), it would seem/appear that the last two characters in the string above, ; and ' would cause the string to be invalid and there for fall into the if and set args.IsValid equal to false. Or, stated another way, since the characters ; and ' are not in the list of valid characters, shouldn't it fall through and set args.IsValid equal to false? Please advise?




function ValidateSearchTxt(source, args) {
                var strSrchTxt = new RegExp(/[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]/);

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

Open in new window

LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
Where is val defined in your function?

Assuming that source should be val i.e
function ValidateSearchTxt(source, args) {
  var strSrchTxt = new RegExp(/[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]/);

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

Open in new window

AND assuming your test string starts with a '.' and not space.

Then your problem is this - your RegEx should be
var strSrchTxt = new RegExp(/^[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]+$/);

Open in new window

The difference being
a) We have the start of string '^' and end of string '$' requirements to match the whole string
b) We have the + after the [...] to tell the parser we want 1 or more of these characters

What you are currently doing is searching the string for any ONE of the specified characters - so as soon as the parser finds ONE the test passes. You want the parser to match ALL the characters in the string.

I would also escape your '.' as this has special meaning for RegEx.
1
 
leakim971PluritechnicianCommented:
// is the litteral version of new RegExp : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/RegExp

var strSrchTxt = new RegExp("your regexp in double quote", "gimuy");
var rgxSrhTxt = /a string/gimuy

Open in new window


the first one is usefull if you want to build the regular expression unlike the second not allowing you to do :
var x = " is ";
var r1 = /"this" + x + "what"/; // <---- fail
when you can do :
var r2 = new RegExp("this" + x + "what","i"); //<--- it's ok
0
 
Garfield SamuelsProject ManagerCommented:
This is what your expression   /[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]/   is doing


Match the character “/” literally
Match a single character present in the list below «[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"
   A character in the range between “a” and “z” (case sensitive) «a-z»
   A character in the range between “A” and “Z” (case sensitive) «A-Z»
   A character in the range between “0” and “9” «0-9»
   A single character from the list “.,/” «.,/»
   The backslash character «\\»
   The literal character “_” «_»
   The literal character “-” «\-»
   A single character from the list “!@#” «!@#»
   The dollar character «\$»
   The literal character “%” «%»
   The literal character “^” «\^»
   The literal character “&” «&»
   The literal character “*” «\*»
   A single character from the list “()” «()»
   The literal character “+” «\+»
   A single character from the list “:"” «:"»
Match the character “/” literally «/»
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Garfiled: So why are ; and ' acceptable characters? Neither of those two characters match any of the options you listed, so I'm not understanding why they are allowed or why they don't force logic into the if statement and set the args.IsValid to false.
0
 
leakim971PluritechnicianCommented:
this is what I see :
function ValidateSearchTxt(source, args) {
                var strSrchTxt = new RegExp(  new RegExp( '[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]' )   );

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

Open in new window


do that make sense for you now ?
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@leak, I'm gonna have to study that for a bit, because right now I don't understand the need for the double RegExp call. Also, when I ran this, it skipped right over (bombed at) the assignment statement: var strSrchTxt = new RegExp(  new RegExp( '[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]' )   ); an bounced completely out of the javascript. I don't think it liked that assignment.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
maybe this will help. this is the entire function:

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

                var strSrchTxt = new RegExp(new RegExp('[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()]'));

                if (!strSrchTxt.test(val)){
                    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;

                }
            }

Open in new window

0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
I changed this:

var strSrchTxt = new RegExp(/[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()]/);

Open in new window


TO:

var strSrchTxt = /[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()]/;

Open in new window


and it gets to the if statement:

if (!strSrchTxt.test(val))

but if ; and or ' are part of the input, it still doesn't go into the if statement and set args.IsValid to false like I think it should.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@leak: I do see what you mean now when you say that you see:

var strSrchTxt = new RegExp(  new RegExp( '[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]' )   );

I was using both notations for the RegExp object. So in other words, use one or the other, but not both like: RegExp(/.../);
0
 
leakim971PluritechnicianCommented:
@leak: I do see what you mean now when you say that you see:

var strSrchTxt = new RegExp(  new RegExp( '[a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"]' )   );

I was using both notations for the RegExp object. So in other words, use one or the other, but not both like: RegExp(/.../);

yes you got it
0
 
leakim971PluritechnicianCommented:
do you still need help there ?

but if ; and or ' are part of the input, it still doesn't go into the if statement and set args.IsValid to false like I think it should.

I don't see ; and ' in your Character set
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@leakim971: yes, that is what I mean. They are not in my character set of acceptable characters. But when the check is done in the if statement, it bounces out of the if, which means to me, it thinks that those two characters are acceptable, when they are not in the character string. So I would think it would set the IsValid flag to false, no?

So if this is my character set:

a-zA-Z0-9.,/\\_\-!@#\$%\^&\*()\+:"

And I input a string to search on that looks like this:

.,dhgf45,/\_-!@#$%^&*()+:";'

shouldn't it fail on ; and or ' and fall through the if statement to the line: args.IsValid = false;?

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

Open in new window

0
 
leakim971PluritechnicianCommented:
check this :
testing
if(false) {
  // never
}
else {
 // always

Open in new window

1
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Julian: I understand what you mean by applying the '^' to my character set. Just curious about what you said about the '.'. On this link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-negated-character-set

 it states that the '.' doesn't need to be escaped in a character set.

Character set. This pattern type matches any one of the characters in the brackets, including escape sequences. Special characters like the dot(.) and asterisk (*) are not special inside a character set, so they don't need to be escaped. You can specify a range of characters by using a hyphen, as the following examples illustrate.

The pattern [a-d], which performs the same match as [abcd], matches the 'b' in "brisket" and the 'c' in "city". The patterns /[a-z.]+/ and /[\w.]+/ match the entire string "test.i.ng".

So does that apply to my situation here? Meaning (I think) that I don't need to escape it?
0
 
Julian HansenConnect With a Mentor Commented:
No you can use it as you have - that was not part of what was causing your RegEx not to work (I do it out of habit - but it is not necessary). The issue with your RegEx was that you were looking for ONE instance of those characters - which it was obviously finding. By making it that the entire string had to match the set fixes the problem.
1
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Thanks to all who contributed!
0
 
Julian HansenCommented:
You are welcome.
0
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.