rules.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * 邮箱
  3. * @param {*} s
  4. */
  5. export function isEmail(s) {
  6. return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
  7. }
  8. /**
  9. * 手机号码
  10. * @param {*} s
  11. */
  12. export function isMobile(s) {
  13. return /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/.test(s)
  14. }
  15. /**
  16. * 用户名
  17. * @param {*} s
  18. */
  19. export function isUserName(s) {
  20. return /^[a-zA-Z0-9\u4e00-\u9fa5]{2,30}/.test(s)
  21. }
  22. /**
  23. * 密码
  24. * @param {*} s 只能输入6-16位,必须包括数字、字母、特殊字符其中两种
  25. */
  26. export function isPwd(s) {
  27. return /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\\(\\)])+$)([^(0-9a-zA-Z)]|[\\(\\)]|[a-z]|[A-Z]|[0-9])[^\\u4e00-\\u9fa5]{6,16}$/
  28. .test(s)
  29. }
  30. /**
  31. * URL地址
  32. * @param {*} s
  33. */
  34. export function isURL(s) {
  35. return /^http[s]?:\/\/.*/.test(s)
  36. }
  37. /**
  38. * 电话号码
  39. * @param {*} s
  40. */
  41. export function isPhone(phone) {
  42. if (!(/^1[3-9]\d{9}$/).test(phone)) {
  43. return {
  44. data: '手机号只能由11位数字组成',
  45. state: false
  46. }
  47. } else {
  48. return {
  49. data: '',
  50. state: true
  51. }
  52. }
  53. }
  54. //中文字符校验
  55. export function isChineseWord(str) {
  56. if (!str) {
  57. return {
  58. data: '您输入的姓名不可以为空',
  59. state: false
  60. }
  61. }
  62. // else if (!(/^[\u4e00-\u9fa5]*$/).test(str)) {
  63. // return {
  64. // data: '姓名只能由中文组成',
  65. // state: false
  66. // }
  67. // }
  68. else {
  69. return {
  70. data: '',
  71. state: true
  72. }
  73. }
  74. }
  75. /**
  76. * 身份证号
  77. * @param {*} s
  78. */
  79. export function isID(sId) {
  80. if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
  81. return {
  82. data: '你输入的身份证长度或格式有误',
  83. state: false
  84. }
  85. }
  86. //身份证城市
  87. var aCity = {
  88. 11: "北京",
  89. 12: "天津",
  90. 13: "河北",
  91. 14: "山西",
  92. 15: "内蒙古",
  93. 21: "辽宁",
  94. 22: "吉林",
  95. 23: "黑龙江",
  96. 31: "上海",
  97. 32: "江苏",
  98. 33: "浙江",
  99. 34: "安徽",
  100. 35: "福建",
  101. 36: "江西",
  102. 37: "山东",
  103. 41: "河南",
  104. 42: "湖北",
  105. 43: "湖南",
  106. 44: "广东",
  107. 45: "广西",
  108. 46: "海南",
  109. 50: "重庆",
  110. 51: "四川",
  111. 52: "贵州",
  112. 53: "云南",
  113. 54: "西藏",
  114. 61: "陕西",
  115. 62: "甘肃",
  116. 63: "青海",
  117. 64: "宁夏",
  118. 65: "新疆",
  119. 71: "台湾",
  120. 81: "香港",
  121. 82: "澳门",
  122. 91: "国外"
  123. };
  124. if (!aCity[parseInt(sId.substr(0, 2))]) {
  125. return {
  126. data: '你的身份证地区有误',
  127. state: false
  128. }
  129. }
  130. // 出生日期验证
  131. var sBirthday = (sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2))).replace(/-/g,
  132. "/"),
  133. d = new Date(sBirthday)
  134. if (sBirthday != (d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate())) {
  135. return {
  136. data: '身份证上的出生日期有误',
  137. state: false
  138. }
  139. }
  140. // 身份证号码校验
  141. var sum = 0,
  142. weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  143. codes = "10X98765432"
  144. for (var i = 0; i < sId.length - 1; i++) {
  145. sum += sId[i] * weights[i];
  146. }
  147. var last = codes[sum % 11]; //计算出来的最后一位身份证号码
  148. if (sId[sId.length - 1] != last) {
  149. return {
  150. data: '你输入的身份证号长度或格式有误',
  151. state: false
  152. }
  153. }
  154. return {
  155. data: '',
  156. state: true
  157. }
  158. }
  159. //获取url参数组成json
  160. export function GetQueryJson(e) {
  161. if(e == 'undefined'){
  162. return {}
  163. }else{
  164. let url=e;//获取url
  165. let arr=[];//存储参数的数
  166. let res={};//存储最终JSON结果对象
  167. arr=url.split('?')[1].split('&');//获取浏览器地址栏中的参数
  168. for(let i=0;i<arr.length;i++){//遍历参数
  169. if(arr[i].indexOf('=')!=-1){//如果参数中有值
  170. let str=arr[i].split('=');
  171. res[str[0]]=str[1];
  172. }else{//如果参数中无值
  173. res[arr[i]]='';
  174. }
  175. };
  176. return res;
  177. }
  178. }