javascript - 关于验证确认密码和纯字母文本是否正确
问题描述
这对于验证确认密码和纯字母文本是否正确。它不工作请帮助。谢谢!
function validation(password, confirmpassword) {
var password = document.form.password.value;
var confirmpassword = document.form.confirmpassword.value;
if (password.length <= 6) {
alert("password must be more thank 6 characters long");
return false;
}
if (password.value !== confirmpassword.value)
alert("password should match");
return false;
}
function allLetter(inputtxt) {
var letters = /^[a-z]*$/i;
if (!inputtxt.value.match(letters)) {
alert("Please input letters only");
}
}
解决方案
Here's how you can do it:
function validation(event) {
event.preventDefault();
var password = document.querySelector(".password").value;
var confirmpassword = document.querySelector(".confirm-password").value;
if (password.length <= 6) {
alert("password must be more thank 6 characters long");
return false;
}
if (password !== confirmpassword) {
alert("password should match");
return false;
}
alert("password valid");
return true;
}
function allLetter(inputtxt) {
var letters = /^[a-z]*$/i;
if (!letters.test(inputtxt)) {
alert("Please input letters only");
}
}
const passwordInputs = document.querySelectorAll("input[type=password]");
const validator = document.querySelector(".validate");
passwordInputs.forEach(function(passwordInput) {
passwordInput.addEventListener("input", function(e) {
allLetter(e.target.value);
});
});
validator.addEventListener("click", function(event) {
console.log("can proceed", validation(event));
});
<form>
<input class="password" type="password"></input>
<input class="confirm-password" type="password"></input>
<button class="validate">Validate</button>
</form>
推荐阅读
- javascript - 直接从相机上传图像到谷歌云存储
- java - 双向 jpa 休眠关系 @OneToMany @ManyToOne 失败,因为未存储父 ID
- bash - 签名错误阻止上传到 S3 存储桶
- python-3.x - TensorFlow 1.15 中的 Autograph 在条件语句中给出 TypeError,在一个分支中赋值
- c# - Update() 和 FixedUpdate() 之间有什么区别,应该如何将这些区别应用于控制运动?
- javascript - 如何禁用滚动功能?
- r - R:使用经度/纬度的散点
- docusignapi - 在单个 API 调用中获取文档信封状态和收件人
- mysql - 如何按顺序连接行而不重复
- java - 当有 3 个客户端连接到 java 中的服务器时,在服务器中运行一些特定代码