我试图创建一个email form字段,它要求用户以hello@domain.ext格式输入电子邮件,但也只允许通过业务电子邮件(不允许使用gmail、yahoo、hotmail等)。
我已经创建了两个独立工作的字段模式,但似乎无法让它们一起工作。
需要hello@domain.ext格式
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"
不允许这些免费电子邮件域。仅限商务电子邮件。
pattern="^(?!.*@(?:live|gmx|yahoo|outlook|msn|icloud|facebook|aol|zoho|yandex|lycox|inbox|myway|aim|goowy|juno|(?:hot|[gy]|google|short|at|proton|hush|lycos|fast)?mail)\.\w+$).*$"
这是我的表格代码:
<form method="POST" action="#">
<input type=hidden name="oid" value="00D70000000KCoG">
<input type=hidden name="retURL"
value="#">
<label for="email">Email</label><input id="email" maxlength="80"
name="email" size="30" type="email"
oninvalid="setCustomValidity('Please enter your business email here.')"
onchange="try{setCustomValidity('')}catch(e){}" pattern="[a-z0-
9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required />
<input type="submit" name="submit" value="Submit">
</form>
最佳答案
以下是两种模式的组合:
pattern="[a-z0-9._%+-]+@(?!(?:live|gmx|yahoo|outlook|msn|icloud|facebook|aol|zoho|yandex|lycox|inbox|myway|aim|goowy|juno|(?:hot|[gy]|google|short|at|proton|hush|lycos|fast)?mail)\.\w+$)[a-z0-9.-]+\.[a-z]{2,4}"
注意,开头的
^
和结尾的$
是不必要的,因为它们是隐式的:pattern
值用^(?:
和)$
包装以匹配整个输入值。请参见regex demo。
详情
^
-隐式-字符串的开头[a-z0-9._%+-]+
-一个或多个字母、数字、.
、_
、%
、+
或-
@
-a@
(?!(?:live|gmx|yahoo|outlook|msn|icloud|facebook|aol|zoho|yandex|lycox|inbox|myway|aim|goowy|juno|(?:hot|[gy]|google|short|at|proton|hush|lycos|fast)?mail)\.\w+$)
-如果模式立即匹配到当前位置的右侧(即@
之后),则前面的负lookahead将失败匹配。[a-z0-9.-]+
-1+小写ascii字母、数字、.
或/和-
\.
-一个点[a-z]{2,4}
-2到4个小写ascii字母。注意:您可能需要将
A-Z
添加到字符类:[a-z0-9._%+-]+
=>[\w.%+-]+
和[a-z0-9.-]+
=>[a-z0-9A-Z.-]+
。