功能:简单的表单验证,密码输入次数控制,定时器控制可输入提醒,密码匹配
Html:请忽略界面设计,没时间做
<form>
<input id="userName" type="text" placeholder="请输入用户名" onblur="checkUserName()"/><br />
<input id="userPassword" type="password" placeholder="请输入密码" onclick="inputPassWord()" onblur="checkPassWord()"/><br />
<input id="verificationCode" style="width:80px" type="text" placeholder="请输入验证码" />
<img style="width: 70px;height: 20px;" src="../../img/verificationCode.jpg"/>
</form>
<button style="margin:20px;background: pink;" onclick="checkCode()">登录</button>
js:
var userName = document.getElementById("userName");
var userPassword = document.getElementById("userPassword");
var verificationCode = document.getElementById("verificationCode");
var inputTimes = 0; //密码输入次数
var canInput = true; //密码框是否可以输入
//登录按钮点击事件
function checkCode(){
if(userName.value == ""){
alert("请输入用户名!");
}else{
if (userPassword.value == "happy123"&&verificationCode.value=="7364") {
alert("登录成功!");
}
if (userPassword.value != "happy123") {
alert("密码错误!");
verificationCode.value = "";
}
if(verificationCode.value!="7364"){
alert("验证码错误!");
}
}
}
//密码输入离开
function checkPassWord(){
inputTimes = inputTimes + 1;
}
//密码输入点击事件
function inputPassWord(){
if (inputTimes>2) {
alert("请三分钟后在尝试!");
//定时器3分钟,inputTimes = 0;
setTimeout(function(){
inputTimes = 0;
},180000);
}
}