easyui validate 后 使用ajax提交

前端之家收集整理的这篇文章主要介绍了easyui validate 后 使用ajax提交前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

继承easyui的default验证,自定义验证规则如下:

(function($){

  1. //计算字符串或者汉字的长度
  2. function getByteLength(s){
  3. var len = 0;
  4. for(i=0;i<s.length;i++)
  5. {
  6. var c = s.substr(i,1);
  7. var ts = escape(c);
  8. if(ts.substring(0,2) == "%u") { len += 2; }
  9. else if(ts== "%B7") { len += 2; }
  10. else { len += 1; }
  11. }//eof:for
  12. return len;
  13. };
  14.  
  15. //jquery-easyUI
  16. $.extend($.fn.validateBox.defaults.rules,{
  17. //校验字符输入的限制(只能输入数字和字母)
  18. lettersandnumericonly:{
  19. validator:function(value,param){
  20. return /^[a-zA-Z0-9]+$/i.test(value);
  21. },message:'{0}只能输入数字和字母'
  22. },//只能输入数字
  23. numericonly:{
  24. validator:function(value,param){
  25. return /^[0-9]+$/i.test(value);
  26. },message:'{0}只能输入数字!'
  27. },//只能输入正整数
  28. positiveInteger:{
  29. validator:function(value,param){
  30. return /^[0-9]+$/i.test(value) && parseInt($.trim(value)) > 0;
  31. },message:'{0}只能输入正整数!'
  32. },//只能输入数字(并且有范围)
  33. numericRange:{
  34. validator:function(value,param){
  35. return /^[0-9]+$/i.test(value) && parseInt($.trim(value)) <= param[1];
  36. },message:'{0}只能输入数字,且不能大于{1}'
  37. },oneToTwoInteger:{
  38. validator:function(value,param){
  39. return /^[0][[.]\d{1,10}]?$/i.test(value)||/^[1]?$/i.test(value)||/^[0]?$/i.test(value);
  40. },message:'{0}只能输入数字,且在0到1之间'
  41. },//验证(-20-130)的整数
  42. integerALLKinds : {
  43. validator : function(value) {
  44. return /^[-|+]?[0-9]+\d*$/i.test(value) && value >= -20 && value <= 130;
  45. },message : '请输入-20到130的任意整数'
  46. },//判断最长
  47. maxLength:{
  48. validator:function(value,param){
  49. return getByteLength($.trim(value)) <= param[1] ;
  50. },message:'{0}长度不能超过{1}个字符或者{2}个汉字'
  51. },//判断最小长度
  52. minLength : {
  53. validator : function(value,param) {
  54. var len = $.trim(value).length;
  55. return len >= param[0];
  56. },message :'{0}长度最小{1}个字符'
  57. },//判断长度范围
  58. length:{validator:function(value,param){
  59. var len=$.trim(value).length;
  60. return len>=param[0]&&len<=param[1];
  61. },message:"{0}内容长度介于{1}和{2}之间."
  62. },//验证年龄
  63. age:{
  64. validator : function(value) {
  65. return /^[0-9]+$/i.test(value) && parseInt($.trim(value)) >= 0 && value>=0 && value <=150;
  66. },message:'年龄格式不对,请输出0-150之间的数字.'
  67. },//验证电话号码
  68. phone : {
  69. validator : function(value) {
  70. return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
  71. },message : '格式不正确,请使用下面格式:020-88888888'
  72. },//验证手机号码
  73. mobile : {
  74. validator : function(value) {
  75. return /^(13|15|18)\d{9}$/i.test(value);
  76. },message : '手机号码格式不正确(正确格式如:13/15/18*********)'
  77. },//验证手机或电话
  78. phoneOrMobile:{
  79. validator : function(value) {
  80. return /^(13|14|15|17|18)\d{9}$/i.test(value) || /^((\(\d{2,4})?$/i.test(value)
  81. || /^\d{8}$/i.test(value);
  82. },message:'请填入手机或电话号码,如13/14/15/17/18*********或020-8888888或88888888'
  83. },//验证身份证
  84. idcard : {
  85. validator : function(value) {
  86. return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
  87. },message : '身份证号码格式不正确'
  88. },//验证是否为小数或整数
  89. floatOrInt : {
  90. validator : function(value) {
  91. return /^(\d{1,3}(,\d\d\d)*(\.\d{1,\d\d\d)*)?|\d+(\.\d+))?$/i.test(value);
  92. },message : '请输入数字,并保证格式正确'
  93. },IntegerOrFloat:{
  94. validator:function(value){
  95. return /^[0-9]+(\.[0-9]+)?$/i.test(value) ;
  96. },message : '请输入数值型数据'
  97. },//验证货币
  98. currency : {
  99. validator : function(value) {
  100. return /^d{0,}(\.\d+)?$/i.test(value);
  101. },message : '货币格式不正确'
  102. },//验证整数
  103. integer : {
  104. validator : function(value) {
  105. return /^[+]?[1-9]+\d*$/i.test(value);
  106. },message : '请输入整数'
  107. },//验证中文
  108. chinese : {
  109. validator : function(value) {
  110. return /^[\u0391-\uFFE5]+$/i.test(value);
  111. },message : '请输入中文'
  112. },//验证英语
  113. english : {
  114. validator : function(value) {
  115. return /^[A-Za-z]+$/i.test(value);
  116. },message : '请输入英文'
  117. },//验证是否包含空格和非法字符
  118. unnormal : {
  119. validator : function(value) {
  120. return /.+/i.test(value);
  121. },message : '输入值不能为空和包含其他非法字符'
  122. },//验证邮政编码
  123. zip : {
  124. validator : function(value) {
  125. return /^[1-9]\d{5}$/i.test(value);
  126. },message : '邮政编码格式不正确'
  127. },//验证IP地址
  128. ip : {
  129. validator : function(value) {
  130. return /d+.d+.d+.d+/i.test(value);
  131. },message : 'IP地址格式不正确'
  132. },//email校验
  133. email:{
  134. validator : function(value){
  135. return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
  136. },message : '请输入有效的电子邮件账号(例:abc@126.com)'
  137. },//校验值是否存在 param[0]: table,param[1]: colum,param[2]: add和修改区分,param[3]:修改前的value
  138. valueExists:{
  139. validator : function(value,param){
  140. var bool = true;
  141. var flag = 0;
  142. if(param[2]){
  143. if(param[2]=='edit'){
  144. if(value == param[3]){
  145. flag = 1 ;
  146. }
  147. }
  148. }
  149. if(flag == 0){
  150. //校验编号是否存在
  151. $.ajax({
  152. url:"url ?table="+param[0]+"&colum="+param[1]+"&value="+value,type:'post',async:false,dataType:'json',success:function(result){
  153. var result = eval(result);
  154. if(result.flag == 0){
  155. bool = true ;
  156. }else{
  157. bool = false;
  158. }
  159. }
  160. });
  161. }
  162. return bool;
  163. },message : '当前资源已被占用'
  164. }
  165. });

})(jQuery)

以上为例子,根据项目需要修改

1、jsp代码

名称
2、js代码

$('#btn_save').bind('click',function(){ if(($("#gfxwjForm").form('validate'))) { saveCommonWin(); } });

3、java代码

猜你在找的Ajax相关文章