javascript中密码验证的If/else语法

If/else syntax on password verification in javascript

本文关键字:else 语法 If 密码 验证 javascript      更新时间:2023-09-26

我对javascript相当陌生,并试图列出失败密码中缺失的字符,以告诉用户他们需要输入什么。

window.onload = function()
{
   var info = document.getElementById("info");
   var test = document.getElementById("myForm").test;
   test.onclick = function(e)
   {
     e.preventDefault();
     var pw = document.getElementById("myForm").pw.value;
     var formula = /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,}/;
     if(formula.test(pw))
     {
         document.getElementById("myForm").submit();
     }
     else if pw.match(/'d/g) == null {
        info.innerHTML = "You need a number";
        console.log(pw.match(/'d/g))
     } else {
       info.innerHTML = "You need a number."
     }
     };
};

以上只是第一次检查用户是否输入了带有现在数字的密码。但是我总是得到

Uncaught SyntaxError: Unexpected identifier

在chrome开发工具与pw。匹配下划线部分。我在网上其他地方看了看,我的语法看起来是正确的。我哪里做错了?

你需要一些括号

else if (pw.match(/'d/g) == null) {
//      ^                       ^           

您需要在if块的条件周围加上括号。

 else if (pw.match(/'d/g) == null) {
    ...

根据:http://www.w3schools.com/js/js_if_else.asp您在else if中漏掉了(and)