电子邮件验证-@之前和点之前的字符长度 [英] Email validation- characters length before @ and before dot
问题描述
我使用以下正则表达式模式来验证可以正常工作的电子邮件地址,但我需要验证@
之前的字符长度,该字符长度应不能少于4个字符.对于@
之后和点.
之前的字符长度,我应该使用相同的规则.
I use the following regex pattern for validating the email address that works fine, BUT I need to validate the length of characters before @
, which should NOT be less than 4 characters. The same rule I should put for the length of characters after @
and before dot .
.
例如,此电子邮件地址无效有效: a@b.c
For example, this email address is NOT valid: a@b.c
但是,这应该是有效的: abcd@abcd.com
However, this one should be valid: abcd@abcd.com
我该怎么办?
这是我目前的尝试:
<ui:define name="validation-tag">
<f:validateRegex
pattern="([\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z])*"
for="contactEmailAddress" />
</ui:define>
推荐答案
我们可以使用带有锚点的正向提前强加长度限制.
We can impose length restrictions using positive look-aheads with anchors.
^(?=[^@]{4,}@)([\w\.-]*[a-zA-Z0-9_]@(?=.{4,}\.[^.]*$)[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z])$
^
和$
将使字符串在开头和结尾处都匹配,然后(?=[^@]{4,}@)
将确保我们在第一个@
之前至少有4个字符,而(?=.{4,}\.[^.]*$)
将确保该部分最后一个.
之前的长度至少为4个符号.
The ^
and $
will make the string match at start and end, then (?=[^@]{4,}@)
will make sure we have at least 4 characters before the first @
, and (?=.{4,}\.[^.]*$)
will make sure the part before the last .
is at least 4 symbols long.
请参见演示
这篇关于电子邮件验证-@之前和点之前的字符长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!