jquery.validate.extend.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*this is basic form validation using for validation person's basic information author:Clara Guo data:2017/07/20*/
  2. $(document).ready(function(){
  3. $.validator.setDefaults({
  4. submitHandler: function(form) {
  5. form.submit();
  6. }
  7. });
  8. //手机号码验证身份证正则合并:(^\d{15}$)|(^\d{17}([0-9]|X)$)
  9. jQuery.validator.addMethod("isPhone",function(value,element){
  10. var length = value.length;
  11. var phone=/^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
  12. return this.optional(element)||(length == 11 && phone.test(value));
  13. },"请填写正确的11位手机号");
  14. //电话号码验证
  15. jQuery.validator.addMethod("isTel",function(value,element){
  16. var tel = /^(0\d{2,3}-)?\d{7,8}$/g;//区号3,4位,号码7,8位
  17. return this.optional(element) || (tel.test(value));
  18. },"请填写正确的座机号码");
  19. //姓名校验
  20. jQuery.validator.addMethod("isName",function(value,element){
  21. var name=/^[\u4e00-\u9fa5]{2,6}$/;
  22. return this.optional(element) || (name.test(value));
  23. },"姓名只能用汉字,长度2-4位");
  24. //校验用户名
  25. jQuery.validator.addMethod("isUserName",function(value,element){
  26. var userName=/^[a-zA-Z0-9]{2,13}$/;
  27. return this.optional(element) || (userName).test(value);
  28. },'请输入数字或者字母,不包含特殊字符');
  29. //校验身份证
  30. jQuery.validator.addMethod("isIdentity",function(value,element){
  31. var id= /^(\d{15}$|^\d{18}$|^\d{17}(\d|X))$/;
  32. return this.optional(element) || (id.test(value));
  33. },"请输入正确的15或18位身份证号,末尾为大写X");
  34. //校验出生日期
  35. jQuery.validator.addMethod("isBirth",function(value,element){
  36. var birth = /^(19|20)\d{2}-(1[0-2]|0?[1-9])-(0?[1-9]|[1-2][0-9]|3[0-1])$/;
  37. return this.optional(element) || (birth).test(value);
  38. },"出生日期格式示例2000-01-01");
  39. //校验IP地址
  40. jQuery.validator.addMethod("isIp",function(value,element){
  41. var ip = /^(?:(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:1[0-9][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:2[0-4][0-9])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[0-9]))$/;
  42. return this.optional(element) || (ip).test(value);
  43. },"IP地址格式示例127.0.0.1");
  44. jQuery.validator.addMethod("notEqual", function(value, element, param) {
  45. return value != param;
  46. }, $.validator.format("输入值不允许为{0}"));
  47. jQuery.validator.addMethod("gt", function(value, element, param) {
  48. return value > param;
  49. }, $.validator.format("输入值必须大于{0}"));
  50. //校验新旧密码是否相同
  51. jQuery.validator.addMethod("isdiff",function(){
  52. var p1=$("#pwdOld").val();
  53. var p2=$("#pwdNew").val();
  54. if(p1==p2){
  55. return false;
  56. }else{
  57. return true;
  58. }
  59. });
  60. //校验新密码和确认密码是否相同
  61. jQuery.validator.addMethod("issame",function(){
  62. var p3=$("#confirm_password").val();
  63. var p4=$("#pwdNew").val();
  64. if(p3==p4){
  65. return true;
  66. }else{
  67. return false;
  68. }
  69. });
  70. //校验基础信息表单
  71. $("#basicInfoForm").validate({
  72. errorElement:'span',
  73. errorClass:'help-block error-mes',
  74. rules:{
  75. name:{
  76. required:true,
  77. isName:true
  78. },
  79. sex:"required",
  80. birth:"required",
  81. mobile:{
  82. required:true,
  83. isPhone:true
  84. },
  85. email:{
  86. required:true,
  87. email:true
  88. }
  89. },
  90. messages:{
  91. name:{
  92. required:"请输入中文姓名",
  93. isName:"姓名只能为汉字"
  94. },
  95. sex:{
  96. required:"请输入性别"
  97. },
  98. birth:{
  99. required:"请输入出生年月"
  100. },
  101. mobile:{
  102. required:"请输入手机号",
  103. isPhone:"请填写正确的11位手机号"
  104. },
  105. email:{
  106. required:"请输入邮箱",
  107. email:"请填写正确的邮箱格式"
  108. }
  109. },
  110. errorPlacement:function(error,element){
  111. element.next().remove();
  112. element.closest('.gg-formGroup').append(error);
  113. },
  114. highlight:function(element){
  115. $(element).closest('.gg-formGroup').addClass('has-error has-feedback');
  116. },
  117. success:function(label){
  118. var el = label.closest('.gg-formGroup').find("input");
  119. el.next().remove();
  120. label.closest('.gg-formGroup').removeClass('has-error').addClass("has-feedback has-success");
  121. label.remove();
  122. },
  123. submitHandler:function(form){
  124. alert("保存成功!");
  125. }
  126. });
  127. //校验修改密码表单
  128. $("#modifyPwd").validate({
  129. onfocusout: function(element) { $(element).valid()},
  130. debug:false, //表示校验通过后是否直接提交表单
  131. onkeyup:false, //表示按键松开时候监听验证
  132. rules:{
  133. pwdOld:{
  134. required:true,
  135. minlength:6
  136. },
  137. pwdNew:{
  138. required:true,
  139. minlength:6,
  140. isdiff:true,
  141. //issame:true,
  142. },
  143. confirm_password:{
  144. required:true,
  145. minlength:6,
  146. issame:true,
  147. }
  148. },
  149. messages:{
  150. pwdOld : {
  151. required:'必填',
  152. minlength:$.validator.format('密码长度要大于6')
  153. },
  154. pwdNew:{
  155. required:'必填',
  156. minlength:$.validator.format('密码长度要大于6'),
  157. isdiff:'原密码与新密码不能重复',
  158. },
  159. confirm_password:{
  160. required:'必填',
  161. minlength:$.validator.format('密码长度要大于6'),
  162. issame:'新密码要与确认新密码一致',
  163. }
  164. },
  165. errorElement:"mes",
  166. errorClass:"gg-star",
  167. errorPlacement: function(error, element)
  168. {
  169. element.closest('.gg-formGroup').append(error);
  170. }
  171. });
  172. });