当两个密码不匹配时,我的 JS 函数不会打印任何内容

如何使密码字段相互检查用户写入的值是否匹配?


function checkPassword(form) {

  pOne = form.pOne.value;

  pTwo = form.pTwo.value;


  // If password not entered 

  if (pOne == '')

    alert("Please enter Password");


  // If confirm password not entered 

  else if (pTwo == '')

    alert("Please enter confirm password");


  // If Not same return False.     

  else if (pOne != pTwo) {

    // alert ("\nPassword did not match: Please try again...") 

    document.querySelector(".submit").addEventListener("click", print)


    function print() {

      return document.querySelector(".pass").textContent = "Your password does not match!"

    }


  }


  // If same return True. 

  else {

    document.querySelector(".submit").addEventListener("click", print)


    function print() {

      return document.querySelector(".pass").textContent = "Your password  match perfectly!"

    }


  }

}

<form class="formtwo" onsubmit="checkPassword(this)">

  <input type="email" name="email" placeholder="Email"><br>

  <input type="password" name="pOne" placeholder="Password">

  <input type="password" name="pTwo" placeholder="Re-Type Password">

  <p class="pass">djkakj</p>

  <button type="submit" class="submit">Submit</button>

</form>


Cats萌萌
浏览 104回答 2
2回答

凤凰求蛊

在测试之前,您需要将事件侦听器添加到表单提交中window.addEventListener("load", function() {&nbsp; document.getElementById("formtwo").addEventListener("submit", function(e) {&nbsp; &nbsp; const pOne = this.pOne.value;&nbsp; &nbsp; const pTwo = this.pTwo.value;&nbsp; &nbsp; const pass = document.querySelector(".pass");&nbsp; &nbsp; let errors = [];&nbsp; &nbsp; // If password not entered&nbsp;&nbsp; &nbsp; if (pOne == '') errors.push("Please enter Password");&nbsp; &nbsp; if (pTwo == '') errors.push("Please enter confirm password");&nbsp; &nbsp; if (pOne != pTwo) errors.push("Password did not match: Please try again...")&nbsp; &nbsp; if (errors.length > 0) {&nbsp; &nbsp; &nbsp; e.preventDefault(); // this will stop submission&nbsp; &nbsp; &nbsp; alert(errors.join("\n"))&nbsp; &nbsp; &nbsp; pass.textContent = "Your password does not match!"&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; pass.textContent = "Your password&nbsp; match perfectly!"&nbsp; })})<form id="formtwo">&nbsp; <input type="email" name="email" placeholder="Email"><br>&nbsp; <input type="password" name="pOne" placeholder="Password">&nbsp; <input type="password" name="pTwo" placeholder="Re-Type Password">&nbsp; <p class="pass">djkakj</p>&nbsp; <button type="submit" class="submit">Submit</button></form>

米琪卡哇伊

你可以看看我的解决方案。希望更容易理解。您的代码中存在一些问题。如果您想在密码不匹配时阻止表单提交,那么您需要使用event.preventDefault来阻止默认行为。您可以触发一次提交事件,然后检查所需的值。const form = document.querySelector('.formtwo');form.addEventListener('submit', checkPassword);function checkPassword(e) {&nbsp; e.preventDefault();&nbsp;&nbsp;&nbsp; let pOne = form.pOne.value;&nbsp; let pTwo = form.pTwo.value;&nbsp; // If password not entered&nbsp; if (pOne == "") alert("Please enter Password");&nbsp; // If confirm password not entered&nbsp; else if (pTwo == "") alert("Please enter confirm password");&nbsp; // If Not same return False.&nbsp; else if (pOne != pTwo) {&nbsp; &nbsp; document.querySelector(".pass").textContent =&nbsp; &nbsp; &nbsp; &nbsp; "Your password does not match!";&nbsp; }&nbsp; // If same return True.&nbsp; else {&nbsp; &nbsp; document.querySelector(".pass").textContent =&nbsp; &nbsp; &nbsp; &nbsp; "Your password&nbsp; match perfectly!";&nbsp; &nbsp;// submitting form&nbsp; &nbsp;form.submit();&nbsp; }}<!DOCTYPE html><html><head>&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width">&nbsp; <title>JS Bin</title></head><body><form class="formtwo">&nbsp; &nbsp; <input type="email" name="email" placeholder="Email"><br>&nbsp; &nbsp; <input type="password" name="pOne" placeholder="Password">&nbsp; &nbsp; <input type="password" name="pTwo" placeholder="Re-Type Password">&nbsp; &nbsp; <p class="pass">Password matching status</p>&nbsp; &nbsp; <button type="submit" class="submit">Submit</button></form></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5