电子邮件验证-@ 之前和点之前的字符长度 [英] Email validation- characters length before @ and before dot
问题描述
我使用以下正则表达式模式来验证工作正常的电子邮件地址,但是我需要验证 @
之前的字符长度,这应该 NOT 少于 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屋!