my_validate1.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. var validate_flag=false;
  2. function my_validate(){
  3. if(mui.os.ios){
  4. mui.each(document.querySelectorAll('.my-validate'), function(index, el) {
  5. el.onblur=function(){
  6. validate_bind(el);
  7. }
  8. });
  9. }else{
  10. mui.each(document.querySelectorAll('.my-validate'), function(index, el) {
  11. el.onkeydown=function(){
  12. var keyNum=window.event ? e.keyCode :e.which; //获取被按下的键值
  13. mui.alert("键值:"+keyNum)
  14. if(keyNum==13){
  15. validate_bind(el);
  16. }
  17. }
  18. });
  19. }
  20. }
  21. function validate_bind(obj){
  22. var tel =/^(0?(13|14|15|18)[\d]{9}$|(01|02|03|04|05|06|07|08|09)[\d]{7,10}$|(400|800)[0-9]{7}$)/; //手机,座机(400-800)
  23. var mobile=/^0?(13|14|15|16|18|19)[0-9]{9}$/; //手机号码
  24. var ch=/^[\u4e00-\u9fa5]{1,}$/;//简体中文
  25. var ch_en_num=/^[A-Za-z0-9_\-\u4e00-\u9fa5]{1,}$/;//简体中文英文数字
  26. var email=/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/; //邮箱
  27. var http=/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+$/; //网址
  28. var doublebyte=/^[^\x00-\xff]+$/; //简体+繁体中文(包括中文全角符号,全角空格)
  29. var positive_int=/^[0-9]\d*$/; //正整数0-9
  30. var negtive_int=/^-[1-9]\d*$/; //负整数
  31. var qq=/^[1-9]([0-9]{4,12})$/; //腾讯QQ号
  32. var postalcode=/^\d{6}$/; //中国邮政编码
  33. var ip=/^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$/;//ip地址
  34. var card_id=/^(\d{17}[\d|x]|\d{15})$/; //身份证(国内)
  35. var full_date=/^\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}$/; //日期格式 (2016\12\12,2016-12-12,2016.12.12)
  36. switch (obj.getAttribute('validate-type')){
  37. case 'tel':
  38. if(tel.test(obj.value)){
  39. validate_flag=true;
  40. }else{
  41. validate_flag=false;
  42. obj.focus();
  43. focus_end(obj)
  44. mui.alert('请输入正确的电话或手机号');
  45. return false;
  46. }
  47. break;
  48. case 'mobile':
  49. if(mobile.test(obj.value)){
  50. validate_flag=true;
  51. }else{
  52. validate_flag=false;
  53. obj.focus();
  54. focus_end(obj)
  55. mui.alert('请输入正确的手机号');
  56. return false;
  57. }
  58. break;
  59. case 'ch':
  60. if(ch.test(obj.value)){
  61. validate_flag=true;
  62. }else{
  63. validate_flag=false;
  64. mui.alert('请输入简体中文');
  65. obj.focus();
  66. focus_end(obj)
  67. return false;
  68. }
  69. break;
  70. case 'ch_en_num':
  71. if(ch_en_num.test(obj.value)){
  72. validate_flag=true;
  73. }else{
  74. validate_flag=false;
  75. mui.alert('请输入中、英文或数字');
  76. obj.focus();
  77. focus_end(obj)
  78. return false;
  79. }
  80. break;
  81. case 'email':
  82. if(email.test(obj.value)){
  83. validate_flag=true;
  84. }else{
  85. validate_flag=false;
  86. mui.alert('请输入正确的邮箱地址');
  87. obj.focus();
  88. focus_end(obj)
  89. return false;
  90. }
  91. break;
  92. case 'http':
  93. if(http.test(obj.value)){
  94. validate_flag=true;
  95. }else{
  96. validate_flag=false;
  97. mui.alert('请输入正确的网址');
  98. obj.focus();
  99. focus_end(obj)
  100. return false;
  101. }
  102. break;
  103. case 'doublebyte':
  104. if(doublebyte.test(obj.value)){
  105. validate_flag=true;
  106. }else{
  107. validate_flag=false;
  108. mui.alert('请输入简体、繁体中文');
  109. obj.focus();
  110. focus_end(obj)
  111. return false;
  112. }
  113. break;
  114. case 'positive_int':
  115. if(positive_int.test(obj.value)){
  116. validate_flag=true;
  117. }else{
  118. validate_flag=false;
  119. mui.alert('请输入正整数');
  120. obj.focus();
  121. focus_end(obj)
  122. return false;
  123. }
  124. break;
  125. case 'negtive_int':
  126. if(negtive_int.test(obj.value)){
  127. validate_flag=true;
  128. }else{
  129. validate_flag=false;
  130. mui.alert('请输入负整数');
  131. obj.focus();
  132. focus_end(obj)
  133. return false;
  134. }
  135. break;
  136. case 'qq':
  137. if(qq.test(obj.value)){
  138. validate_flag=true;
  139. }else{
  140. validate_flag=false;
  141. mui.alert('请输入正确的QQ号');
  142. obj.focus();
  143. focus_end(obj)
  144. return false;
  145. }
  146. break;
  147. case 'postalcode':
  148. if(postalcode.test(obj.value)){
  149. validate_flag=true;
  150. }else{
  151. validate_flag=false;
  152. mui.alert('请输入正确的邮政编码');
  153. obj.focus();
  154. focus_end(obj)
  155. return false;
  156. }
  157. break;
  158. case 'ip':
  159. if(ip.test(obj.value)){
  160. validate_flag=true;
  161. }else{
  162. validate_flag=false;
  163. mui.alert('请输入正确的IP地址');
  164. obj.focus();
  165. focus_end(obj)
  166. return false;
  167. }
  168. break;
  169. case 'card_id':
  170. if(card_id.test(obj.value)){
  171. validate_flag=true;
  172. }else{
  173. validate_flag=false;
  174. mui.alert('请输入正确的身份证号');
  175. obj.focus();
  176. focus_end(obj)
  177. return false;
  178. }
  179. break;
  180. case 'full_date':
  181. if(full_date.test(obj.value)){
  182. validate_flag=true;
  183. }else{
  184. validate_flag=false;
  185. mui.alert('请输入正确的日期格式');
  186. obj.focus();
  187. focus_end(obj)
  188. return false;
  189. }
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. function focus_end(obj){
  196. var len = obj.value.length;
  197. if (document.selection) {
  198. var sel = obj.createTextRange();
  199. sel.moveStart('character', len);
  200. el.collapse();
  201. sel.select();
  202. } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
  203. obj.selectionStart = obj.selectionEnd = len;
  204. }
  205. }