简体   繁体   English

Javascript密码字段比较

[英]Javascript Password field comparison

Hello i have the following code. 您好,我有以下代码。

        <script type="text/javascript">
            function validateForm() {
                var u = document.forms["myForm"]["username"].value;
                if (u == null || u == "") {
                    alert("First name must be filled out");
                    return false;
                }
                else 
                    var x = document.forms["myForm"]["email"].value;
                    var atpos = x.indexOf("@");
                    var dotpos = x.lastIndexOf(".");
                    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
                    {
                        alert("Not a valid e-mail address");
                        return false;
                    }

                    else 
                        var p = document.forms["myForm"]["pwd1"].value;
                    if ( p < 7)
                    {
                        alert("Not a valid password");
                        return false;
                    }

            }


</script>

    Username: <input type="text" name="username"><br />
    Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input id="email" type="text" /><br />
    Password&nbsp;
    <input id="pwd1" type="password" /><br />
    Confirm&nbsp;&nbsp;&nbsp;
    <input id="Password2" type="password" /><br />
    <br />
   <input type="submit" value="Submit"><br />






</div>
</form>

which is run with visual studio 13 asp runat server. 使用Visual Studio 13 ASP Runat Server运行。 Now my problem is that the other 2 (username and email) if they aren't correct it displays the warning and notifies the user (aka me ) that something is wrong. 现在我的问题是,其他2个(用户名和电子邮件)如果不正确,则会显示警告并通知用户(又名我)出了点问题。

the password field only does that if its emtpy with the current code and not if i even put a single a character then it redirects me to the 404 which would be natural if it would be 7 characters and above. 密码字段仅在它与当前代码为空的情况下才这样做,而不是在我什至只输入一个字符的情况下,它会将我重定向到404,如果它是7个字符及以上,那将是很自然的。

I have no idea why this is happening and have had the past 6 hours trying to figure out a solution or a mistake in my code via some already implemented forms or examples. 我不知道为什么会这样,并且在过去的6个小时中,我试图通过一些已经实现的形式或示例来找出我的代码中的解决方案或错误。

Any help appreciated thank you. 任何帮助表示感谢,谢谢。

If you are checking for length of the password the code should be as follow 如果要检查密码的长度,则代码应如下所示

var p = document.forms["myForm"]["pwd1"].value.length;

instead of

var p = document.forms["myForm"]["pwd1"].value;

It might help you!! 它可能会帮助您!

function validate()
{
  var a = document.getElementById("pwd1");
  var b = document.getElementById("pwd2");
  if(!(a==b))
  {
    alert("both passwords are not matching");
    return false;
  }
  return true;
}

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

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