f:validateRegex输入无效字符后不显示任何消息 [英] f:validateRegex doesn't show any message after typing invalid character
问题描述
我正在使用JSF.带有用于验证的代码:
I'm using JSF. With code for validating:
<h:inputText id="sender" value="#{contactForm.sender}">
<f:validateRegex pattern="([a-zA-Z_0-9]).{4,32}" />
</h:inputText>
当我输入字符串时:abc.结果无效.好的. 然后我输入:abcd @.我以为该字符串无效,因为该字符串包含@ letter.但是我没有收到任何消息.这意味着该字符串是有效的.我对正则表达式不太清楚.你能帮忙吗?
When i type a string: abc. The result is invalid. Good. Then i type: abcd@ . I thought, that string is invalid because that string contains @ letter. But i don't recieve any messages. That means that string is valid. I don't understand about regex clearly. Can you help?
推荐答案
您忘记了转义与输入字符串中任何字符匹配的.
元字符
You forgot to escape the .
meta-character which matches any character in the input string
([a-zA-Z_0-9]).{4,32}
// ^
// |
// |
// Notice the dot . this matches any character
因此,您的正则表达式将匹配任何以小写,大写字符或下划线_
或0
和9
之间的数字开头的字符串,然后是重复4至32次的任何字符,因此这几乎匹配任何内容
So you regular expression will match any string starting with lowercase, uppercase character or an underscore _
or a number between 0
and 9
followed by any character repeated between 4 and 32 times, so this almost matches anything.
要解决此问题,您必须转义点.
并将其包含在第一组中,如下所示,-当然可以使用\w
代替[a-zA-Z_0-9]
,因为它们相同也是-:
To fix this you have to escape the the dot .
and include in the first group, something like the following, - of course you can use \w
instead of [a-zA-Z_0-9]
as they are the same thing too -:
([\w\.]){4,32}
.
点必须进入组内,否则表达式将匹配与以前相同的任何字符串,但随后包含4到32个点,而现在它将匹配包含字母字符或数字或点或下划线,长度在4到32个字符之间.
The .
dot has to go inside the group or the expression will match any string starting with the same as before but then contains between 4 and 32 dots, whereas now it will match any string that contains alphabetical characters or numbers or dots or underscores with a length between 4 and 32 characters.
这篇关于f:validateRegex输入无效字符后不显示任何消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!