aquery验证
Posted 呱呱的呱呱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了aquery验证相关的知识,希望对你有一定的参考价值。
- // 手机号码验证
- jQuery.validator.addMethod("mobile", function(value, element) {
- var length = value.length;
- var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/
- return this.optional(element) || (length == 11 && mobile.test(value));
- }, "手机号码格式错误");
- // 电话号码验证
- jQuery.validator.addMethod("phone", function(value, element) {
- var tel = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
- return this.optional(element) || (tel.test(value));
- }, "电话号码格式错误");
- // 邮政编码验证
- jQuery.validator.addMethod("zipCode", function(value, element) {
- var tel = /^[0-9]{6}$/;
- return this.optional(element) || (tel.test(value));
- }, "邮政编码格式错误");
- // QQ号码验证
- jQuery.validator.addMethod("qq", function(value, element) {
- var tel = /^[1-9]\d{4,9}$/;
- return this.optional(element) || (tel.test(value));
- }, "qq号码格式错误");
- // IP地址验证
- jQuery.validator.addMethod("ip", function(value, element) {
- var ip = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
- return this.optional(element) || (ip.test(value) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256));
- }, "Ip地址格式错误");
- // 字母和数字的验证
- jQuery.validator.addMethod("chrnum", function(value, element) {
- var chrnum = /^([a-zA-Z0-9]+)$/;
- return this.optional(element) || (chrnum.test(value));
- }, "只能输入数字和字母(字符A-Z, a-z, 0-9)");
- // 中文的验证
- jQuery.validator.addMethod("chinese", function(value, element) {
- var chinese = /^[\u4e00-\u9fa5]+$/;
- return this.optional(element) || (chinese.test(value));
- }, "只能输入中文");
- // 下拉框验证
- $.validator.addMethod("selectNone", function(value, element) {
- return value == "请选择";
- }, "必须选择一项");
- // 字节长度验证
- jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
- var length = value.length;
- for (var i = 0; i < value.length; i++) {
- if (value.charCodeAt(i) > 127) {
- length++;
- }
- }
- return this.optional(element) || (length >= param[0] && length <= param[1]);
- }, $.validator.format("请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)"));
//回显select下拉选的值(很实用)
<select name="sex" id="select">
<option value="0">男</option>
<option value="1">女</option>
</select>
<script type="text/javascript">
var sex = ${show.sex};
alert(sex)
//获取id是select的select属性
var s = document.getElementById("select");
//获得该sex下的所有option元素,返回的是一个数组
var user_sex = s.getElementsByTagName("option");
for(var i=0;i<user_sex.length;i++){
if(user_sex[i].value==sex){
user_sex[i].selected = true;
}
}
//使用jq-validate插件校验
$("#form1").validate({
rules:{
uname:{
required:true,
minlength:2,
},
upwd:{
required:true,
minlength:5
},
upwd1:{
required:true,
equalTo:"#upwd"
},
phone:{
digits:true,
rangelength:[11,11]
}
},
messages:{
uname:{
required:‘用户名不能为空‘,
minlength:"长度至少为2",
},
upwd:{
required:‘用户密码不能为空‘,
minlength:"长度至少为5"
},
upwd1:{
required:‘请再次输入密码‘,
equalTo:"两次输入的密码不一样"
},
phone:{
digits:"请输入正确的号码",
rangelength:"请输入正确的长度"
}
}
})
</script>
//回显单选按钮的值(很实用)
<input type="radio" name="quanxian" id="auth" value="1" ${show.quanxian==1? "checked=‘checked‘":"" } />经理
//保证输入的格式是2012-2-12
<script type="text/javascript">
$("#form1").validate({
rules:{
time:{
required:true,
checkTimeExist:true
}
},
messages:{
time:{
required:‘这是必填字段‘,
checkTimeExist:‘请输入正确的格式‘
}
}
})
jQuery.validator.addMethod("checkTimeExist", function(value, element){
var ereg = /^(\d{1,4})(-)(\d{1,2})(-)(\d{1,2})$/;
var r = value.match(ereg);
if (r == null) {
return false;
}
var d = new Date(r[1], r[3] - 1, r[5]);
var result = (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[5]);
return this.optional(element) || (result);
}, "请输入正确的日期");
</script>
以上是关于aquery验证的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Gym 100739 AQueries