表單校驗
阿新 • • 發佈:2017-11-04
長度 username clas 電話 增加 use rul action pwd
1、form表單驗證
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>驗證</title> | |
</head> | |
<body> | |
<form id="myForm" action="success.html" method="post"> | |
用戶名:<input name="userName" placeholder="請輸入用戶名"> |
|
<button type="button" onclick="doSubmit()">登錄</button> | |
</form> | |
<!-- | |
可以跳過驗證: | |
F12 | |
之後把type="button" 改成type="submit" | |
--> | |
<script type="text/javascript" src="../js/jquery-1.8.3.js"></script> | |
<script type="text/javascript"> |
|
function doSubmit(){ | |
var userName=$("[name=‘userName‘]").val(); | |
if(userName==""){ | |
alert("用戶名不能為空"); | |
return; | |
} | |
//表單提交 | |
$("#myForm").submit(); | |
} | |
</script> | |
</body> | |
</html> |
2、success
<!DOCTYPE html> | |
<html> |
|
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<h1>登錄成功</h1> | |
</body> | |
</html> |
3、validate驗證框架
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>驗證框架的使用</title> | |
<style type="text/css"> | |
.error{ | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<form action="success.html" method="post" id="myForm"> | |
用戶名:<input name="userName"> <br/> | |
密碼:<input name="password" type="password" id="pwd"> <br/> | |
確認密碼:<input name="repPassword"type="password" > <br/> | |
郵箱:<input name="email"> <br/> | |
手機號:<input name="phone"> <br/> | |
性別:<input name="sex" type="radio" value="男" checked>男 | |
<input name="sex" type="radio" value="女">女<br/> | |
是否同意協議<input type="checkbox" name="context"><br/> | |
<button type="submit">註冊</button> | |
</form> | |
<!--引入需要的js庫--> | |
<script type="text/javascript" src="../js/jquery-1.8.3.js"></script> | |
<script type="text/javascript" src="../js/jquery.validate.js"></script> | |
<script type="text/javascript"> | |
$(function(){ | |
//form表單的驗證事件 | |
$("#myForm").validate({ | |
//驗證規則 需要對哪些元素做驗證 | |
rules:{ | |
userName:{ | |
required:true | |
}, | |
password:{ | |
required:true, | |
minlength:6, | |
maxlength:10 | |
}, | |
repPassword:{ | |
required:true, | |
minlength:6, | |
maxlength:10, | |
equalTo:"#pwd" | |
}, | |
email:{ | |
required:true, | |
email:true | |
}, | |
phone:{ | |
required:true, | |
checkPhone:true //自己書寫的手機驗證正則 | |
}, | |
context:{ | |
required:true | |
} | |
}, //rules end | |
//不符合驗證規則的錯誤信息 | |
messages:{ | |
userName:{ | |
required:"請輸入用戶名" | |
}, | |
password:{ | |
required:"請輸入密碼", | |
minlength:"密碼長度不能少於6位", | |
maxlength:"密碼長度不能大於10位" | |
}, | |
repPassword:{ | |
required:"請輸入密碼", | |
minlength:"密碼長度不能少於6位", | |
maxlength:"密碼長度不能大於10位", | |
equalTo:"兩次密碼輸入不一致" | |
}, | |
email:{ | |
required:"請輸入郵箱", | |
email:"郵箱格式不正確" | |
}, | |
phone:{ | |
required:"請輸入手機號", | |
checkPhone:"電話號碼格式不正確"//自己書寫的手機驗證正則 | |
}, | |
context:{ | |
required:"您沒有同意協議" | |
} | |
}, // messages end | |
//鼠標失去焦點立即驗證 | |
onfocusout:function(element){ | |
$(element).valid(); | |
} | |
}); // end validate | |
//增加了手機驗證正則 | |
jQuery.validator.addMethod("checkPhone",function(value,element){ | |
var tel = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/ | |
return this.optional(element) || (tel.test(value)); | |
},"電話號碼格式不正確") | |
}) | |
</script> | |
</body> | |
</html> |
表單校驗