首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Jquery表单查验

2012-10-25 
Jquery表单检验link typetext/css relstylesheet href/eitp/styles/successOrFailure.css/scr

Jquery表单检验
<link type="text/css" rel="stylesheet" href="/eitp/styles/successOrFailure.css"/>
<script type="text/javascript" src="<s:url  value='/scripts/jquery/jquery.validate.js' />"></script>
<script type="text/javascript" src="/eitp/scripts/jquery/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript"/>
$(document).ready(function(){
//验证
if($("#messageBox").length>0){
$("#messageBox").hide();
}
$.validator.setDefaults({      
submitHandler: function(form) {  

form.submit();   
    }      
}); 
$("#teacherForm").validate({
errorLabelContainer: "#messageBox",
debug:true,
wrapper: "li",
rules:{
"teacherInfo.name":{required:true,byteRangeLength:[4,30]},
"teacherInfo.birth":{required:true,dateISO:true},
"teacherInfo.familyPhone":{isPhone:true},
"teacherInfo.phone":{isMobile:true},
"teacherInfo.zipCode":{isZipCode:true},
"teacherInfo.familyAddr":{byteRangeLength:[2,200]},
"teacherInfo.emailAddr":{email:true},
"teacherInfo.personalMotto":{byteRangeLength:[1,200]}
},
messages:{
"teacherInfo.name":{required:'<s:text name="basicInfo.item.validatorName"/>',byteRangeLength:'<s:text name="index.item.validatorName"/>'},
"teacherInfo.birth":{required:'<s:text name="basicInfo.item.validatorBirth"/>',dateISO:'<s:text name="basicInfo.item.validatorBirthDate"/>'},
"teacherInfo.familyPhone":{isPhone:'<s:text name="basicInfo.item.validatorFamilyPhone"/>'},
"teacherInfo.phone":{isMobile:'<s:text name="basicInfo.item.validatorMobile"/>'},
"teacherInfo.zipCode":{isZipCode:'<s:text name="basicInfo.item.validatorZipCode"/>'},
"teacherInfo.familyAddr":{byteRangeLength:'<s:text name="basicInfo.item.validatorFamilyAddress"/>'},
"teacherInfo.emailAddr":{email:'<s:text name="basicInfo.item.validatorEmail"/>'},
"teacherInfo.personalMotto":{byteRangeLength:'<s:text name="basicInfo.item.validatorPersonalMotto"/>'}

}
});

//长度验证
$.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.addMethod("isPhone", function(value, element) {    
  var tel = /^(\d{3,4}-?)?\d{7,9}$/g;    
  return this.optional(element) || (tel.test(value));    
});

//手机号码验证
$.validator.addMethod("isMobile", function(value, element){
    var length = value.length;
    return this.optional(element) || length == 11 && /^1[358]\d{9}$/.test(value);
});

// 邮政编码验证    
$.validator.addMethod("isZipCode", function(value, element) {    
  var tel = /^[0-9]{6}$/;    
  return this.optional(element) || (tel.test(value));    
});

//设置头像
$("#teacherPhoto").dialog({
autoOpen:false,
width:400,
height:400,
resizable: false,
title:'<fmt:message key="student.item.chooseReallyPhoto"/>'
});
});
</script>
--------------------------------------

热点排行