Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

converting regex to proper format for HTML5 input validation pattern

I am trying to use an HTML form <input> to validate to conform to a string with 7-30 characters and requires at least one letter and one number (but use of other characters is allowed).

The vendor that holds our account data provided me with this regular expression for password validation: (?=.{7,30})(?=.*[a-zA-Z])(?=.*[\d])(?!.*(?:=))

When I try to use this like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<input type="password" name="register_password" pattern="(?=.{7,30})(?=.*[a-zA-Z])(?=.*[\d])(?!.*(?:=))" required>

…nothing I enter into the field will validate. It just throws this no matter what I try:

html5 password validation not passing

Would someone please let me know what the proper format for the pattern attribute should be?

>Solution :

Your pattern is entirely lookaheads, it doesn’t have anything to match the input string itself. Since the pattern attribute is automatically anchored at both ends (it must match the entire string), this means it will only match a zero-length input that also begins with 7-30 characters and contains a letter and number — obviously these are contradictory.

Take .{7,30} out of the lookahead and put it after the other lookaheads.

<form>
  <input pattern="(?=.*[a-zA-Z])(?=.*\d)(?!.*(?:=)).{7,30}">
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading