如何重置文本框并使文本框中的值等于"" (或空白)? (JavaScript的)

时间:2016-07-29 23:12:42

标签: javascript passwords

我写了几个函数来检查两个密码是否相等。我先输入两个密码。当我点击"验证密码"它应该显示"密码匹配"或"需要第二次尝试。请再次在第一个密码框中输入您的密码,因为这两个密码不匹配"取决于密码是否彼此相等。如果两个密码不相等,则消息"需要第二次尝试。请再次在第一个密码框中输入您的密码,因为这两个密码不匹配"被展示。我还希望第一个密码框(password1)在两个密码不匹配时变为空白(我希望它重置)。但是,这在我的代码中不起作用。我在这里做错了什么?

我使用了password.js文件和setpassword.html文件。

我的password.js文件是这样的:

var verifypasswordclick = document.getElementById("txtPWVerified");

function verifypassword1() {
    var password1 = document.getElementById("txtPassword").value;
    var verifypassword = document.getElementById("txtPWVerified").value;
    if(password1 == '' || verifypassword == '') {
        return null;
    }
    if(password1 == verifypassword) {
        alert('The passwords match');
    }
    if(password1 !== verifypassword || password1 == "" || verifypasword == "") {
        alert("A second try is needed. Please enter your password in the first password box again because the two passwords don't match");
    }
    if(password1 !== verifypassword || password1 == "" || verifypasword == "") {
        password1 = "";
    }
}
verifypasswordclick.addEventListener("blur",verifypassword1);

我的setpassword.html文件是:

<!DOCTYPE html>
<!-- H5FormValidation.html -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Register Here</title>
</head>

<body>
  <h2>Register Here</h2>

  <form id="formTest" method="get" action="processData">
    <table>

    <tr>
      <td><label for="txtEmail">Email<span class="required">*</span></label></td>
      <td><input type="email" id="txtEmail" name="email" required></td>
    </tr>
    <tr>
      <td><label for="txtPassword">Password<span class="required">*</span></label></td>
      <td><input type="password" id="txtPassword" name="password" required></td>
    </tr>
    <tr>
      <td><label for="txtPWVerified">Verify Password<span class="required">*</span></label></td>
      <td><input type="password" id="txtPWVerified" name="pwVerified" required></td>
    </tr>

    <tr>
      <td>&nbsp;</td>
      <td>
          <input type="reset" value="CLEAR" id="btnReset"></td>
    </tr>
    </table>
  </form>
 <script src = "password.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您只需设置密码元素的值(由于元素的ID)

document.getElementById("txtPassword").value = '';

答案 1 :(得分:0)

更改密码.js代码如下。 // PS:使用警报

的用户不是很友好
var verifypasswordclick = document.getElementById("txtPWVerified");
function verifypassword1() {
        var password1 = document.getElementById("txtPassword")
        var verifypassword = document.getElementById("txtPWVerified");
        //do nothing until the fields is field
        if(password1.value && verifypassword.value){
            if(password1.value === verifypassword.value) {
                alert('The passwords match');
            }else{
                alert("A second try is needed. Please enter your password in the first password box again because the two passwords don't match");
                password1.value= "";

            }
        }
    }
verifypasswordclick.addEventListener("blur",verifypassword1);
祝你好运