简体   繁体   English

验证密码

[英]Validating Password

I'm trying to validate a password using javascript, It's to make sure that when changing the password, the new password entered is equal to that of the re-entering of the new password (user is asked to enter their new password twice so both have to match) but at the same time, i want to make sure that the new password is at least 6 characters long, I have these functions separately but don't know how to combine them... thanks for help in advance! 我正在尝试使用javascript验证密码,以确保在更改密码时,输入的新密码等于重新输入新密码的密码(要求用户输入两次新密码,因此必须匹配),但与此同时,我想确保新密码的长度至少为6个字符,我分别拥有这些功能,但不知道如何将它们组合在一起...预先感谢您的帮助!

This is what i have so far... 这是我到目前为止所拥有的...

This is to make sure the new passwords match: 这是为了确保新密码匹配:

function validatePassword()
    {
     var new_password = document.getElementById("new_password").value;
     var confirm_new_password = document.getElementById("confirm_new_password").value;
     <!-- if they match, go to next page -->
        if ( new_password == confirm_new_password)
          {
             return true;
          }
     <!-- if they don't match, an error message is displayed -->
        else
          {
               alert("Passwords do not match.");
          }
             return false;
    }

This is for length of password: 这是密码的长度:

 function validatePassword()
    {
    if (document.getElementById("new_password").value.length < "5")
    {
    <!--If pasword is less than 5 characters long, display error message-->
    alert("Please ensure your password is at least 6 characters long.");
    return false;
    }
    return true;
    }

How do i combine both of these to form a SINGLE function where the two new passwords are checked so that they match, and also check that they are longer than 6 characters? 如何将这两个密码组合在一起以形成一个SINGLE功能,在此功能中将检查两个新密码以使其匹配,并且还要检查它们是否超过6个字符?

To just combine your two functions, this would work: 只需将您的两个功能结合起来,就可以了:

function validatePassword()
{
     var new_password = document.getElementById("new_password").value;
     var confirm_new_password = document.getElementById("confirm_new_password").value;

     if (new_password.length < 5)
     {
         <!--If pasword is less than 5 characters long, display error message-->
         alert("Please ensure your password is at least 6 characters long.");
         return false;
     }
     else if ( new_password != confirm_new_password)
     {
         alert("Passwords do not match.");
         return false;
     }
     else
     {
          return true;
     }
 }

Although I agree, there are better procedures out there. 尽管我同意,但是还有更好的程序。 And please, make sure you're doing server-side validation as well since client-side validation is very easy to skip around. 并且请确保您也在进行服务器端验证,因为客户端验证很容易跳过。

im not sure but you can call validatePassword() this function inside 我不确定,但是您可以在内部调用validatePassword()这个函数

if ( new_password == confirm_new_password)
{
    validatePassword();
}

You have two options, either make the two functions a single function, or make them two separate functions and call them both before you submit / process your form. 您有两个选择,可以将两个功能设置为单个功能,也可以将两个功能设置为单独的功能,然后在提交/处理表单之前将它们都调用。

if (validatePasswordLength() && validatePasswordsMatch()) {
    // Continue
}

you have to try this code that is small and working. 您必须尝试使用​​此小巧有效的代码。

if(document.getElementById("new_password").value != document.getElementById("confirm_new_password").value){
     alert("Passwords do not match.");
    return false;
}
<script>
function validatePassword()
    {
     var new_password = document.getElementById("new_password").value;
     var confirm_new_password = document.getElementById("confirm_new_password").value;

    if (document.getElementById("new_password").value.length < "5")
    {
    alert("Please ensure your password is at least 6 characters long.");
    return false;
    }
     if (new_password == confirm_new_password)
     {
           alert("Password no match");
           return false;
     }
     return true;
    }
</script>
<form action="" onsubmit="return validatePassword()">
<p>New Password: <input type="password" id="new_password" name="new_password" /></p>
<p>Confirm Password: <input type="password" id="confirm_new_password" name="confirm_new_password" /></p>
<p><input type="submit" value="submit" /></p>
</form>

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

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