1

Here is my form.

<form method="post" name="frm" >

    <label>Name*<input type="text" name="Name" value="<?php echo $r['Name'] ?>" onblur="if(this.value.length<3) alert('Name too short');" /></label>


    <label>Username*<input type="text" name="UN" value="<?php echo $r['UN'] ?>" onblur="if(this.value.length<5) alert('Username too short');" /></label>
    <label>Password*<input type="password" name="PW"  onblur="validation()" /></label>
    <label>Confirm Password*<input type="password" name="CM" onblur="validation()" /></label>
    <?php } ?>

    <input type="submit" name="Submit" value="<?php if($new) echo 'Register'; else echo 'Update'; ?>" />
</form>

Without writing seperate onblur events I am trying to get all these events into a function called validation() as I have done for Password and confirm Password fields.Here is my validation function.

<script language="javascript">
      function validation(){
            var password= document.frm.PW.value;
            var password2=document.frm.CM.value;
            if (password.length<5){
                alert ("Password too short");
                }
            else if(password!=password2){
                alert("password mismatch");
                }

            }

    </script>

But with this code once I have filled password and when I am about to start inputting for confirm password field it alerts the message("password mismatch").How to get rid of this.And for all the form tags if I am validating and using validation() function then in each tag do I have to call onblur=validation();

4

1 回答 1

1

不要在主密码更改时触发密码验证 - 当您更改密码 2 或单击提交时触发它。:)

如果您在更新密码和密码 2 时坚持检查,我会添加一个检查以查看密码 2 是否留空:

else if(password !== password2 && password2 !== ""){

但是对提交进行所有检查是一个更清洁的解决方案(正如 Rake 所说,更新跨度而不是警报会更整洁)。

于 2013-07-29T14:30:09.593 回答