簡體   English   中英

驗證密碼字段和確認密碼字段

[英]Validation of the password field and the confirm password field

晚安。 我正在開發一個研究項目,我真的很想做密碼字段的驗證和 confirmSenha 字段的驗證,這個想法是當用戶輸入不同的密碼時。 返回一條消息說密碼不匹配。 下面是我的代碼的 scope。

function validarPasswordConfirm(password, confirmPassword) {

  var typedpassword = document.getElementsByName('password').value;
  var confirmTypedPassword = document.getElementsByName('passwordConfirm').value;

  if(typedpassword !== confirmTypedPassword) {
    return { value: false, text: 'Passwords are not the same.' }
  } else {
    return { value: true, text: '' }
  }
}

我正在嘗試使用 confirmpassword 字段驗證密碼字段

function getElementsByName 返回一個數組,因此您不能像那樣訪問 value 屬性。

我建議改用 getElementById。

我注意到您的代碼存在一些問題。


首先,不再使用var查看原因)。 您應該使用替代方法。

var的替代品如下。

  1. let - 將它用於可以更改的變量。
  2. const - 將其用於無法更改的變量(常量)。

其次, getElementsByName()返回一個NodeList ,它是一個可迭代的(類似於array )。 這意味着.valueNodeListundefined

要修復它,請將您的代碼更改為以下內容。

const typedPassword = document.querySelector("#password").value;
const confirmTypedPassword = document.querySelector("#passwordConfirm").value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM