表單
<form id="form1" action="#" method="post">
賬號<input id="UserName" style="height:20px; width:100px" type="text" /><br/>
密碼<input id="Password" style="height:20px; width:100px" type="password" /><br/>
<input id="Submit1" type="submit" value="登陸" onClick="return chek()"/><br/>
<input id="Submit2" type="submit" value="重置" /><br/>
</form>
在登陸按鈕的表單元素添加一個onClick鼠標點擊事件,調用函數check();
javascript驗證函數
function chek()
{
var name = form1.UserName.value;
if((name.length > 10) || (name == ""))
{
alert("用戶名不合格!");
form1.UserName.focus(); //返回表單元素位置
return false;
}
var pwd = form1.Password.value;
if((pwd.length < 6) || (pwd == ""))
{
alert("密碼不規範!");
form1.Password.focus();
return false;
}
}
驗證效果