简体   繁体   English

JavaScript验证-关于“确认密码”字段

[英]JavaScript Validation - about the Confirm Password field

I am making a sign up page and I am instructed to add client side validation and javascript validation. 我正在制作一个注册页面,并指示我添加客户端验证和javascript验证。 In the HTML file(signup.html), I have 8 input fields, 7 of those have regular expression, but only 4 have required field. 在HTML文件(signup.html)中,我有8个输入字段,其中7个具有正则表达式,但只有4个具有必填字段。 I am having a problem about the "Confirm Password" field. 我对“确认密码”字段有疑问。 Even though the input is different from the "Password" field, the validation process still continues, instead of alerting the user that the password must be the same to proceed. 即使输入的内容与“密码”字段不同,验证过程仍然继续,而不是警告用户密码必须相同。

Here is the HTML code for the form: 这是表单的HTML代码:

            <form method="POST" action="http://webdevfoundations.net/scripts/formdemo.asp" id="signUp" onSubmit="confirmForm(this);">
                <fieldset id="personalInfo">
                    <legend>
                        Personal Information
                    </legend>
                    <table class="userInput">
                        <tr>
                            <td class="label">
                                <label for="firstName">
                                    First Name 
                                </label>
                            </td>
                            <td>
                                <input type="text" name="first" id="firstName" pattern="[a-zA-Z]{2,}" title="Must have at least 2 characters of alphabet" maxlength="50" autofocus/>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="lastName">
                                    Last Name
                                </label>
                            </td>
                            <td>
                                <input type="text" name="last" id="lastName" pattern="[a-zA-Z]{2,}" title="Must have at least 2 characters of alphabet" maxlength="50"/>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="email">
                                    Email<span class="asterisk">*</span>
                                </label>
                            </td>
                            <td>
                                <input type="email" name="email" id="email" pattern="\w.*+\@+[a-z]+\.[a-z]{2,7}" title="Example: abc@yahoo.com" required/>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <label for="age">
                                    Age
                                </label>
                            </td>
                            <td>
                                <input type="number" name="age" id="age" pattern="[1-9]{1,2}" title="Numeric value must be used" min="3"/>
                            </td>
                        </tr>
                    </table>
                </fieldset> 

                <br/>

                <fieldset id="genderField">
                    <legend>
                        Gender
                    </legend>
                    <input type="radio" name="gender" id="genderM" value="Male">
                    <label for="genderM">
                        Male
                    </label>

                    <input type="radio" name="gender" id="genderF" value="Female">
                    <label for="genderF">
                        Female
                    </label>                                
                </fieldset>

                <br/>

                <fieldset id="logInfo"> 
                    <legend>
                        Log In Information
                    </legend>
                    <table class="userInput">
                        <tr>
                            <td>
                                <label for="userName">
                                    Username<span class="asterisk">*</span>
                                </label>
                            </td>
                            <td>
                                <input type="text" name="user" id="userName" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{5,}" title="Must have at least 5 characters, including UPPER/lowercase letters and number" required/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="password">
                                    Password<span class="asterisk">*</span>
                                </label>
                            </td>
                            <td>
                                <input type="password" name="pword" id="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{5,}" title="Must have at least 5 characters, including UPPER/lowercase letters and number" required/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="confirmP">
                                    Confirm Password<span class="asterisk">*</span>
                                </label>
                            </td>
                            <td>
                                <input type="password" name="pwordConfirm" id="confirmP" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{5,}" title="Must be the same password as above" required/>
                            </td>
                        </tr>
                    </table>
                </fieldset>

                <br />
                <input type="submit" value="Sign Up"/>
            </form>

Here is the JavaScript code: 这是JavaScript代码:

function confirmForm(form)
{
        if(!(form.first.value.match(/^[a-zA-Z]{2,}$/))) {
        alert("Invalid First Name");
        form.first.focus();
            return false;
    }

    if(!(form.last.value.match(/^[a-zA-Z]{2,}$/))) {
        alert("Invalid Last Name");
        form.last.focus();
        return false;
    }

    if(!(form.email.value.match(/^\w.*+@[a-z]+\.[a-z]{2,7}$/))) {
        alert("Invalid Email");
        form.email.focus();
        return false;
    }

    if(!(form.age.value.match(/^[1-9]{1,3}$/))){
        alert("Invalid Age");
        form.age.focus
        return false
    }

    if(!(form.user.value.match(/^\w{5,}$/))) {
        alert("Invalid Username");
        form.user.focus();
        return false;
    }

    if(!(form.pword.value.match(/^.\w{5,}$/))){
        alert("Invalid Password");
        form.pword.focus();
        return false;
    }

    if(!(form.pwordConfirm.value == form.pword.value)){
        alert("Password must be the same");
        form.pwordConfirm.focus();
        return false;
    }
    else {
        return true;
    }
}

you need to do return function confirmForm() on form tag something like this. 您需要在form标签上执行返回函数confirmForm()这样的操作。

id="signUp" onSubmit="return confirmForm(this);">
//-----use return here--^

Also use test instead of match which returns true or false on success or failure of valdation. 还可以使用test而不是match来进行testmatch成功或失败时将返回truefalse

var patt1 = /^[a-zA-Z]{2,}$/;
if(!(patt1.test(form.first.value)

The emal validation pattern is also wrong, remove * from email pattern emal验证模式也有误,请从电子邮件模式中删除*

should be 应该

/^\w.+\@+[a-z]+\.[a-z]{2,7}$/

instead of 代替

/^\w.*+@[a-z]+\.[a-z]{2,7}$/

Here is the complete JSfiddle DEMO 这是完整的JSfiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM