最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Invalid regular expression in HTML5 input pattern in email input - Stack Overflow

programmeradmin3浏览0评论

I need to use this regular expression in a HTML input (Email Type), I used to create a regex here to match what we wanted to validate

Regex

/^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$/gm

Pattern

^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$

Element with Pattern

<input type="email" pattern="^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$" name="email" class="form-control" placeholder="email address" value="<%= session("user_email_addr") %>" required="required" title="Please input a valid email">

And now I am getting this error (Invalid Escape) when I input any email in the form

Pattern attribute value ^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$/: Invalid escape

How can I made this work?

I need to use this regular expression in a HTML input (Email Type), I used to create a regex here to match what we wanted to validate http://regexr./3gib9

Regex

/^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$/gm

Pattern

^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$

Element with Pattern

<input type="email" pattern="^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$" name="email" class="form-control" placeholder="email address" value="<%= session("user_email_addr") %>" required="required" title="Please input a valid email">

And now I am getting this error (Invalid Escape) when I input any email in the form

Pattern attribute value ^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.(|net|org|co|org)+((\.[A-Z|a-z|0-9]{2,4})?)$/: Invalid escape

How can I made this work?

Share Improve this question asked Aug 15, 2017 at 15:08 ricardoriosricardorios 3861 gold badge8 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You need to remove unnecessary escaping backslashes and wrong | inside character classs, and just redundant constructs:

pattern="([A-Za-z0-9][._]?)+[A-Za-z0-9]@[A-Za-z0-9]+(\.?[A-Za-z0-9]){2}\.(?|net|org)+(\.[A-Za-z0-9]{2,4})?"

See a demo below:

input:valid {
  color: green;
}
input:invalid {
  color: red;
}
<form name="form1"> 
 <input pattern="([A-Za-z0-9][._]?)+[A-Za-z0-9]@[A-Za-z0-9]+(\.?[A-Za-z0-9]){2}\.(?|net|org)+(\.[A-Za-z0-9]{2,4})?" title="Wrong email!"/>
 <input type="Submit"/> 
</form>

Details

  • ^...$ - the anchors are not necessary in HTML5 pattern since the regex is anchored by default (^(?: and )$ are added to the pattern automatically when translated into a regex)
  • [A-Z|a-z|0-9] - the | inside a character class is treated as a literal | symbol, not as an alternation operator
  • (\.|_){0,1} - the single char alternation makes little sense when one can use a character class, [._] is a more natural construct here. Besides, {0,1} is more naturally written as ? quantifier.
  • \@ - the Culprit! - this char does not have to be escaped, and can't be escaped in an ES6 regex pattern that has a u modifier (this is a regex that HTML5 pattern translates into, e.g. pattern="[A-Z]" will be translated into /^(?:[A-Z])$/u regex)
  • (|net|org|co|org) can be reduced to (?|net|org): ? matches and co (thus, co alternative may be removed) and org is repeated twice.

There might be other enhancements here, but you can also just use

type="email"

HTML5 email validation attribute.

发布评论

评论列表(0)

  1. 暂无评论