Search code examples
regexhtmlhtml-input

Regular Expression pattern : alphanumeric(compulsory) , some special characters (optional)


I have this:

<input type="password" value="" pattern="^(?=.{8,}$)(([a-zA-Z0-9])\2?(?!\2))+$" required/>

The non consecutive repeating characters work fine but I am having trouble making it strictly alphanumeric and allow some special characters( ! $ ( ) , - . : ; ? @ { } [ ] ^_ "). The special characters are not compulsory but okay if inputted by user.

summary:

1.Must be between 8 and 30 characters 2.Must not contain 3 consecutive repeating characters 3.Must have a minimum of 1 letter 4.Must have a minimum of 1 number 5.Only the following special characters are allowed: ! $ ( ) , - . : ; ? @ { } [ ] ^_ ~ `"


Solution

  • I am having trouble making it strictly alphanumeric and allow some special characters

    You can use this regex:

    ^(?=\D*\d)(?=.*?[a-zA-Z])(?=[\w!$(),.:;?@{}\[\]^-]{8,}$)((.)\2?(?!\2))+$