一对regexp测试的自定义消息失败

时间:2014-01-27 08:56:16

标签: javascript regex

无法弄清楚如何在密码测试regexp上显示自定义消息。

关于submition,我有下一个正则表达式可以正常工作:

[a-zA-Z]{1,5}    -> min 6 or symbols. must contain digits
[0-9]{1,5}       -> min 6 or symbols. must contain letters

这真的很奇怪,但下一个:

[^\n]{6,}        -> min 6 or more symbols (means that having letters and digits)

如果我尝试用以下代替:

[a-zA-Z0-9]{1,5}

它不起作用。

所以当我使用时:

[a-zA-Z]{6,}     -> digit is needed (we have 6 symbols)
[0-9]{6,}        -> letter is needed (we have 6 symbols)

他们也没有通过。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

    var ar=[/[a-zA-Z]{1,5}/,/[a-zA-Z]{6,}/,/[0-9]{6,}/,/[^\n]{6,}/];
    var msg=["min 6 or symbols. must contain digits",
             "min 6 or more symbols (means that having letters and digits)",
             "digit is needed (we have 6 symbols)",
            "letter is needed (we have 6 symbols)",
            "min 6 or symbols. must contain digits"];
    var line="123456";
    for(i=0;i<ar.length;i++){ 
             if(ar[i].test(line))
               {  alert(msg[i]);
                     break;
               }

}

<强> FIDDLE DEMO