[英]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
的替代品如下。
let
- 将它用于可以更改的变量。const
- 将其用于无法更改的变量(常量)。 其次, getElementsByName()
返回一个NodeList
,它是一个可迭代的(类似于array
)。 这意味着.value
在NodeList
中undefined
。
要修复它,请将您的代码更改为以下内容。
const typedPassword = document.querySelector("#password").value;
const confirmTypedPassword = document.querySelector("#passwordConfirm").value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.