0

我正在为密码表单提供验证功能。我需要能够实现一些验证规则并在提交时检查它们。现在对我来说代码是正确的,但我认为它们可能是我的代码中的一些逻辑错误,我太累了,无法注意到(咖啡机也是如此!)

这是JavaScript:

<script type="text/javascript">
<!--
    function validate(registerForm)
    registerForm.onsubmit=function()
    {
        var pw1 = document.forms["register"]["password1"].value;
        var pw2 = document.forms["register"]["password2"].value;

        //Check values are present in both fields
        if(pw1 == '' || pw2 == '')
        {
            alert("Please enter your password twice.");
            return false;
        }
        //Check there no spaces
        else if(document.forms["register"]["password1"].value.indexOf(invalid) > - 1) 
        {
            alert("Spaces are not allowed in passwords!");
            return false;
        }
        //Check passwords are the same
        else
        {
            if(pw1 != pw2) 
            {
                alert("The passwords you entered were not the same. Please try again!");
                return false;
            }
            //Accept passwords
            {
                alert("Password accepted!");
                return true;
            }
        }
    }
-->
</script>

以及与之配套的 HTML 表单:

<form id="register">
    <label for="username">Username</label>
    <input type="text" class="input_text" name="username" id="name" placeholder="e.g. AberLibrary01" />
    <br />
    <label for="password">Password</label> 
    <input type="text" class="input_text" name="password1" id="password1" placeholder="e.g. aber01" />
    <br />
    <label for="re-enterpassword">Re-enter password</label>
    <input type="text" class="input_text" name="password2" id="password2" placeholder="e.g. aber01" />

    <input type="submit" class="button" value="Register" />
    </form>

    <script type="text/javascript">
    <!--
        new validate(document.forms['register']);
    -->
    </script>

任何可爱的 StackOverflow 社区的想法?确切的问题是它不会检查密码中的空格或输入的两个密码是否相同。它成功地检查了两个密码字段中至少有一些东西。

谢谢丹

4

2 回答 2

0

这一行:

else if(document.forms["register"]["password1"].value.indexOf(invalid) > - 1) 

invalid未定义,我怀疑这会导致您面临的问题。

于 2012-07-03T15:08:18.563 回答
0

对您的代码进行了更改使其正常工作http://jsbin.com/igonec/edit#preview

错误

  1. 使用 var pw1 = document.forms["register"]["password1"]。它导致了错误
  2. 缺别的。
  3. 使用无效而不是“”。
  4. 错误使用括号。

我省略了您的错误并使解决方案更加优雅。

Javascipt

    function validate()
     {
        var pw1 = document.getElementById("password1").value;
        var pw2 = document.getElementById("password2").value;

        //Check values are present in both fields
        if(pw1 ==='' || pw2 === '')
        {
            alert("Please enter your password twice.");
            return false;
        }
        //Check there no spaces
        else if(document.getElementById("password1").value.indexOf(" ") > - 1) 
        {
            alert("Spaces are not allowed in passwords!");
            return false;
        }
        //Check passwords are the same
        else
        {
            if(pw1 !== pw2) 
            {
                alert("The passwords you entered were not the same. Please try again!");
                return false;
            }
            else
            {
                alert("Password accepted!");
                return true;
            }
        }

    }

HTML

<form id="register">
    <label for="username">Username</label>
    <input type="text" class="input_text" name="username" id="name" placeholder="e.g. AberLibrary01" />
    <br />
    <label for="password">Password</label> 
    <input type="text" class="input_text" name="password1" id="password1" placeholder="e.g. aber01" />
    <br />
    <label for="re-enterpassword">Re-enter password</label>
    <input type="text" class="input_text" name="password2" id="password2" placeholder="e.g. aber01" />

    <input type="submit" class="button" onclick="validate()" value="Register" />
    </form>
于 2012-07-03T15:11:12.260 回答