surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.11k stars 801 forks source link

Regex validator not validating when using case-insensitive flag #7620

Closed gregpawlowski closed 8 months ago

gregpawlowski commented 8 months ago

Are you requesting a feature, reporting a bug or asking a question?

Bug

What is the current behavior?

Regex validator doesn't validate for case insensitive flag

What is the expected behavior?

Should validate

How would you reproduce the current behavior (if this is a bug)?

Enter the following validator in suvejs json:

     "validators": [
      {
       "type": "regex",
       "text": "Please enter a valid something.com email",
       "regex": "/.+@something.com/i"
      }
     ],
     "inputType": "email",

When entering a test such as "test@SOMETHING.COM" the validator fails. I have tried using // instead of one /. It does not appear that case insensitive match is working.

Specify your

andrewtelnov commented 8 months ago

@gregpawlowski Interesting. I was not able to make it works either. Our code is pretty simple: new RegExp(this.regex); and then re.test(value). We will take a look.

Thank you, Andrew

gregpawlowski commented 8 months ago

Thank You @andrewtelnov , The problem is when using / in the valus.

RegExp constructor will sanitize string, flags are passed as second argument. Onlu using regex literal in javscript allows for // and passing flags at the end.

From console: new RegExp("/.@something.com/i") returns: /\/.@something.com\/i/

However passing the i as a second argument new RegExp(".@something.com", "i") return /.@something.com/i

Only regex literal syntax allows for passing in flags /.

Maybe a check can be made on the value? Check if beginning and end have / and pass in flags?

Alternatively documentation should be updated that flags cannot be passed only regex strings not objects. This would be unfortunate, one could make a custom validator for these cases. Or add a new parameter for flags in the regex validator.

andrewtelnov commented 8 months ago

@gregpawlowski I decided to implement the easiest possible solution for now. We may need a better code if more flags someone wish to set. However, at the current moment "i" is the only flag that I beleive somebody decides to use.

Thank you, Andrew

andrewtelnov commented 8 months ago

@gregpawlowski At the end, we decided to add a boolean property "insensitive".

Thank you, Andrew

yanhuiyi commented 3 weeks ago

Hi @andrewtelnov, I got the same issue, how can I add literal value in regexp? For example, I would check if an email ending with .edu domain. It should be case insensitive. Thanks!