简体   繁体   中英

Regex [data parsley pattern] Allow special characters

apologies my regex skills are lacking.

I'm using parsleyjs & I have this rule:

export const PASSWORD_RULE5 = /[\[\]@$!%*?&£€^(){}:"|<>/]{1}/;

This works. The rule is applied, if one of those characters does not appear in the password it highlights and warns them they need to add a special character.

the problem is the Input has a data_parsley_pattern :

data_parsley_pattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&£€^(){}:"|<>])[A-Za-z\\d!$%@#£€*?&]{8,}$"

This allows a password with

  • At least 8 characters
  • At least 1 uppercase char
  • At least 1 lowercase char
  • At least 1 numeric
  • At least 1 special character

If they enter a ^ in their password, it no longer highlights, but the data_parsley_pattern still fails.

No matter what I try I cannot get this to work.

I've tried adding the new allowed special characters here & also tried escaping them, but the rule still fails.

^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&**£€^(){}:"|<>/**])[A-Za-z\\d!$%@#£€*?&]{8,}$

Or

^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&**£€\\^\\(\\)\\{\\}\\:\"\|\\<\\>\\/**])[A-Za-z\\d!$%@#£€*?&]{8,}$

Do I need to treat these characters differently?

Thanks for any insight.

Thanks to comments from @JvdV, I realised I was missing out my special characters in an important part of the regex.

Thanks To @Pradeep, the tool you provided in the comment is a massive help when testing Regex.

Another issue I had was that I am using ASP.Net MVC and an @Html.PasswordFor therefore the escape characters are slightly different.

Here's the solution for my issue above:

data_parsley_pattern = "^(?=. [az])(?=. [AZ])(?=. \d)(?=. [\\\"@$!% ?&£€^(){}:|<>/])[A-Za-z\d!$%@#£€ ?&^(){}\\\":|<>/]{8,}$"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM