Search code examples
htmlregexpasswords

Password pattern wont accept this special characters like <>,./'";


I use this pattern in input tag type password to accept a strong password. "Must contain at least one number, one uppercase and lowercase letter, and one special character ,minimum of 8 characters, and maximum of 26 characters". But when I inserted one of these characters <>,./'"; the field title keep appearing. I would like to implement all special characters in my registration form

<form>
  <input pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$" />
  <input type="submit" />
</form>


Solution

  • (?=.*[0-9])         require one digit anywhere
    (?=.*[a-z])         require one lowercase letter anywhere
    (?=.*[A-Z])         require one uppercase letter anywhere
    (?=.*[^0-9a-zA-Z])  require one symbol anywhere
    ^.{8,26}$           match any string of 8 to 26 characters
    

    Combine all of these together and you get:

    ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^0-9a-zA-Z]).{8,26}$
    

    This, of course, counts anything that isn't a letter from A to Z or a digit as a special character, including accented letters, letters from other alphabets, and whitespace.