使用javascript的代码出错

时间:2014-04-29 02:09:44

标签: javascript

我的用户名和密码验证代码出现了错误,我不确定如何修复它 我正在尝试使用

function checkForm(form) {
    if (form.username.value == "") {
        alert("Error: Username cannot be blank!");
        form.username.focus();
        return false;
    }
    re = /^\w+$/;
    if (!re.test(form.username.value)) {
        alert("Error: Username must contain only letters, numbers and underscores!");
        form.username.focus();
        return false.pwd2.value) {
        if (form.pwd1.value.length < 8) {;
        }

        if (form.pwd1.value != "" && form.pwd1.value == form alert("Error: Password must contain at least eight characters!"); form.pwd1.focus();
            return false;
        }
        if (form.pwd1.value == form.username.value) {
            alert("Error: Password must be different from Username!");
            form.pwd1.focus();
            return false;
        }
        re = /[0-9]/;
        if (!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one number (0-9)!");
            form.pwd1.focus();
            return false;
        }
        re = /[a-z]/;
        if (!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one lowercase letter (a-z)!");
            form.pwd1.focus();
            return false;
        }
        re = /[A-Z]/;
        if (!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one uppercase letter (A-Z)!");
            form.pwd1.focus();
            return false;
        }
    } else {
        alert("Error: Please check that you've entered and confirmed your password!");
        form.pwd1.focus();
        return false;
    }

    alert("You entered a valid password: " + form.pwd1.value);
    return true;
}

我正在尝试将此代码用于此html

 <form name="hw4Form" action="" autocomplete="off">
<br>
<fieldset name="LoginInfo">
<input id="username" type="text" name="username" placeholder="username" size="30">
<br>
<br>
Password:
<br>
<input id="pwd1" type="password" name="pwd1" placeholder="password" required="required" size="30">
<span id="pwd1Hint" class="hint">Password is too short (must be at least 8 characters)</span>
<br>
Repeat Password:
<br>
<input id="pwd2" type="password" name="pwd2" placeholder="password" required="required" size="30">
<span id="pwd2Hint" class="hint">Passwords don't match</span>
<br>
<br>

我的代码编辑软件出错,但我无法弄清楚如何修复它 我还想确保这个javascript适用于html

2 个答案:

答案 0 :(得分:0)

看起来像这一行:

 if(form.pwd1.value != "" && form.pwd1.value == form

应该是:

 if(form.pwd1.value != "" && form.pwd1.value == form.username.value)) {

或类似的东西。

在您的HTML中,您还需要使用</form>

关闭表单

答案 1 :(得分:-1)

我建议您使用console.log(表单)或其他字段来检查js执行过程。或者为什么不使用firebug来调试该函数。