日期:2014-05-16  浏览次数:20360 次

javascript验证登陆页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

<script type="text/javascript">
function validateform() {

var username = document.getElementById("username").value;
var password = document.getElementById("password").value;

if(username.trim().length == 0) {
//alert("用户名必须输入");
document.getElementById("errorforusername").innerHTML = "请输入用户名";
document.getElementById("username").focus();
return false;
} else if(password.length == 0) {
//alert("密码必须输入");
document.getElementById("errorforpassword").innerHTML = "请输入密码";
document.getElementById("password").focus();
return false;
}
return true;
}

function clearError(element){
document.getElementById(element).innerHTML = "";
}

function submitForm(){

//validate
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;

if(username.trim().length == 0) {
//alert("用户名必须输入");
document.getElementById("errorforusername").innerHTML = "请输入用户名";
document.getElementById("username").focus();
return;
} else if(password.length == 0) {
//alert("密码必须输入");
document.getElementById("errorforpassword").innerHTML = "请输入密码";
document.getElementById("password").focus();
return;
}

document.getElementById("myform").submit();
}

</script>

</head>
<body>
<noscript>
提示:您的浏览器不支持或禁止了网页脚本,无法正常注册
</noscript>
<form id="myform" onsubmit="return validateform()" action="validate.jsp" method="post">

UserName:  <input type="text" name="" id="username" onkeydown="clearError('errorforusername')" onblur="validateform()" autocomplete="off"/>
<span style="color:red" id="errorforusername"></span>
<br/>
Password: <input type="password" name="" id="password" onkeydown="clearError('errorforpassword')" onblur="validateform()"/>
<span style="color:red" id="errorforpassword"></span>
<br/>
<input type="button" value="保存" onclick="submitForm()" />
</form>


</body>
</html>