Dominik Zinser
Dominik Zinser

Reputation: 134

Regex pattern in HTML5 example solution for regular expression

I am testing the patterns-attribute for input elements and I built a regex that should do the following: for testing if the input is a valid email address. Is it possible to group various regex expressions like I did in HTML5?

I tried to group like this:

pattern="[ ([a-zA-Z0-9+-.]{1,}) ([@]{1}) ([a-zA-Z0-9+-.]{1,}) ([.]{1}) ([a-zA-Z0-9+-.]{1,}) ]{5,254}"
  1. Any chars from a-z and A-Z and 0-9 and +-. are allowed with a minimum of 1 char.
  2. It should be followed by a @.
  3. Any chars from a-z and A-Z and 0-9 and +-. are allowed with a minimum of 1 char.
  4. It should be followed by a dot ..
  5. Any chars from a-z and A-Z and 0-9 and +-. are allowed with a minimum of 1 char.
  6. In total are 5-254 chars allowed.

Upvotes: 0

Views: 139

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174796

The below regex would satisfy all your conditions. You don't need to add space within the pattern,

^(?=.{5,254}$)[a-zA-Z0-9+-.]{1,}@[a-zA-Z0-9+-.]{1,}[.]{1}[a-zA-Z0-9+-.]{1,}

Add a single character in the demo input and you could see the difference.

DEMO

Upvotes: 1

Related Questions