1

I'm trying to validate an email input via HTML5 pattern.

The input field must follow this format:

[email protected]

In other words I need to check if there is a string of letters + a dot + another string of letters + must contain the string simon.

So far I've found

pattern=".*simon.+" 

However, it only checks if the input contains simon.

Help would be highly appreciated!

7
  • stackoverflow.com/questions/5601647/… Commented Jun 7, 2018 at 19:14
  • You can trial-and-error your RegEx in regex101.com , It even has break-down explanation of what does your RegEx actually means Commented Jun 7, 2018 at 19:14
  • regex101.com is broken. Commented Jun 7, 2018 at 19:15
  • @sln What do you mean? It works for me. Commented Jun 7, 2018 at 19:16
  • Try pattern="\S+\.\S+@\S*simon\S*\.\S+" Commented Jun 7, 2018 at 19:19

1 Answer 1

1

You may use

pattern="\S+\.\S+@\S*simon\S*\.\S+"

The pattern gets parsed as /^(?:\S+\.\S+@\S*simon\S*\.\S+)$/ and matches

  • ^ - start of string
  • (?: - start of a non-capturing container group
  • \S+\.\S+ - 1+ non-whitespace chars, . and 1+ chars other than whitespace
  • @ - a @ char
  • \S* - 0+ non-whitespace chars
  • simon - a literal substring
  • \S* - 0+ non-whitespace chars
  • \. - a dot
  • \S+ - 1+ non-whitespace chars
  • ) - end of the container group
  • $ - end of string.

HTML5:

input:valid {
  color: black;
}
input:invalid {
  color: red;
}
<form name="form1"> 
 <input pattern="\S+\.\S+@\S*simon\S*\.\S+" title="Please enter an email address with 'simon' in the domain name!" placeholder="[email protected]" value="[email protected]" />
 <input type="Submit"/> 
</form>

If you want to "controll" the dots in the pattern, replace \S with [^\s.] that matches any char but a whitespace and ..

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this very useful explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.