我创建了一个简单的Javascript验证脚本:
var el = document.getElementById("username");
var el_pwd = document.getElementById("password");
var el2 = document.getElementById("feedback");
var el3 = document.getElementById("ok");
var el4 = document.getElementById("ok2");
function checkUsername() {
var username = el.value;
var password = el_pwd.value;
if((username.length < 5) & (password.length <= 0)) {
el2.className = 'warning';
el2.textContent = "Username Not long enough yet..";
el2.style.color = "red";
} else {
el2.textContent = " ";
}
}
function checkPassword() {
var username = el.value;
var password = el_pwd.value;
if((username.length >= 5) & (password < 7) ) {
el2.textContent = "Password MUST be 7 or more characters";
el2.style.color = "red";
} else if ((username.length <= 4) & (password.length <= 0)) {
el2.className = 'warning';
el2.textContent = "Username Not long enough yet..";
el2.style.color = "red";
} else {
el2.textContent = " ";
}
}
function usernameOK() {
var username = el.value;
if(username.length >= 5) {
el3.style.display="block";
} else {
el3.style.display = "none";
}
}
function passwordOK() {
var password = el_pwd.value;
if(password.length >= 7) {
el4.style.display="block";
} else {
el4.style.display = "none";
}
}
function feedBack() {
el2.className = 'tip';
el2.textContent = "The username MUST be at least 5 characters";
el2.style.color = "blue";
}
el.addEventListener("focus", feedBack, false);
el.addEventListener("blur", checkUsername, false);
el.addEventListener("blur", usernameOK, false);
el_pwd.addEventListener("focus", checkPassword, false);
el_pwd.addEventListener("blur", passwordOK, false);
我想要做的是当用户输入所需数量的字符时,在密码输入旁边显示绿色勾号。
我有它工作,绿色勾号出现在“模糊”,但我不知道如何按照我想要的方式。
我猜它有关于keyPress或keyDown的事情。
这是jsfiddle,以更好地了解我想要实现的目标:
http://jsfiddle.net/addiosamigo/89zfo94m/6/
抱歉,它有点笨重(我已经在代码审查中询问了我可以做些什么来使其更具功能性)但我还在学习。任何输入都将非常感谢!
答案 0 :(得分:1)
在添加侦听器时,只需将“blur”替换为“keyup”:
el.addEventListener("keyup", checkUsername, false);