实现功能:当你输入用户名和密码时自动调用javaScript来验证你的username,password是否符合要求。
function check(){
var objUsername = document.forms[0].username; //获得usernamevar objPassword1 = document.forms[0].password1; //获得password
var objPassword2 = document.forms[0].password2;
if(objUsername.value.length<6 ||objUsername.value.length>12){
document.getElementById("userMsg").innerHTML = "<font color='red'>用户名必须为6—12位</font>";
objUsername.innerText=""; //把文本框中的文本置为""
document.getElementById("username").focus(); //得到文本框的焦点
}
if(objPassword1.value.length<6 ||objPassword1.value.length>12){
document.getElementById("pswMsg1").innerHTML = "<font color='red'>密码必须为6—12位</font>";
objPassword1.innerText="";
document.getElementById("password1").focus();
}
if(objPassword1.value != objPassword2.value){
document.getElementById("pswMsg2").innerHTML = "<font color='red'>两次密码不一致</font>";
objPassword2.innerText="";
document.getElementById("password2").focus();
}
}
在index.html 中
<body >
<form method="post">
username:<input type="text" name="username" id="username" οnblur="check()"><span id="userMsg"></span><br>
password1:<input type="text" name="password" id="password1" οnblur="check()"><span id="pswMsg1"></span><br>
password2:<input type="text" name="password" id="password2" οnblur="check()"><span id="pswMsg2"></span><br>
<input type="button" value="ok" >
</form>
</body>
然后用浏览器输入你的网页地址对此进行校验。