您好,我正在尝试设置一个 RegexpValidator,它只接受长度在 6-30 个字符之间的一串字母数字字符,并且需要一个数字。我是正则表达式新手,我尝试过的所有操作似乎都不断返回无效的 ValidationRsultEvent。这是一段代码:
<mx:RegExpValidator id="regexValidator" source="{passwordInput}" property="text"
triggerEvent="" valid="onPasswordValidate(event)" invalid="onPasswordValidate(event)" />
private function validateRegister():void
{
regexValidator.expression = "^(?=.*(\d|\W)).{6,30}$";
regexValidator.validate();
}
最佳答案
我不确定什么会导致验证错误,但就您的正则表达式而言,要将字母数字字符串与至少一个数字匹配,请尝试 ^(?=.*\d)\w{ 6,30}$
^ # Match begining of string
(?=.*\d) # Lookahead, assert there is any number of characters followed by a digit
\w{6,30} # \w matches letters, digits and the underscore character, 6-30 of them
$ # Match End of string
如果您只想匹配字母和数字,可以使用 [0-9a-zA-Z]
而不是 \w
。
您当前的正则表达式 ^(?=.*(\d|\W)).{6,30}$
匹配至少包含一个除 之外的字符的任何字符串[a-zA-Z_]
(\d|\W
匹配数字或“非单词”字符),长度在 6 到 30 个字符之间,不一定满足您指定的要求。
关于regex - 需要 Flex Regex 验证表达式作为密码,仅允许字母数字值并且需要一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2665547/