rules.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import Vue from 'vue'
  2. // 手机号验证
  3. var mobile = (rule, value, callback) => {
  4. let regFormat = /^[1][3456789][0-9]{9}$/ //正确手机号
  5. if (!value) {
  6. return callback(new Error('不能为空'))
  7. }
  8. if (!(regFormat.test(value))) {
  9. callback(new Error('请输入正确手机号'))
  10. } else {
  11. callback()
  12. }
  13. }
  14. //数字验证
  15. var number = (rule, value, callback) => {
  16. if (!value) {
  17. return callback(new Error('不能为空'))
  18. }
  19. if (!Number.isInteger(value)) {
  20. callback(new Error('请输入数字值'))
  21. } else {
  22. if (value < 18) {
  23. callback(new Error('必须大于18岁'))
  24. } else if (value > 55) {
  25. callback(new Error('必须小于55岁'))
  26. } else {
  27. callback()
  28. }
  29. }
  30. }
  31. //text不能为空
  32. var text = (rule, value, callback) => {
  33. if (!value) {
  34. return callback(new Error('不能为空'))
  35. } else {
  36. callback()
  37. }
  38. }
  39. //desc不能为空
  40. var desc = (rule, value, callback) => {
  41. if (!value) {
  42. return callback(new Error('不能为空'))
  43. } else {
  44. callback()
  45. }
  46. }
  47. //邮箱
  48. var email = (rule, value, callback) => {
  49. let mal = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
  50. if (!value) {
  51. return callback(new Error('不能为空'))
  52. }
  53. if (!(mal.test(value))) {
  54. callback(new Error('请输入正确邮箱'))
  55. } else {
  56. callback()
  57. }
  58. }
  59. //开始时间
  60. var startTime = (rule, value, callback) => {
  61. if (!value) {
  62. return callback(new Error('请选择开始日期'))
  63. } else {
  64. callback()
  65. }
  66. }
  67. //开始时间
  68. var endTime = (rule, value, callback) => {
  69. if (!value) {
  70. return callback(new Error('请选择结束日期'))
  71. } else {
  72. callback()
  73. }
  74. }
  75. //时间
  76. var time = (rule, value, callback) => {
  77. if (!value) {
  78. return callback(new Error('请选择日期'))
  79. } else {
  80. callback()
  81. }
  82. }
  83. //多选框
  84. var checkbox = (rule, value, callback) => {
  85. if (value.length < 1) {
  86. return callback(new Error('请选择一个'))
  87. } else {
  88. callback()
  89. }
  90. }
  91. //单选框
  92. var radio = (rule, value, callback) => {
  93. if (value.length < 1) {
  94. return callback(new Error('请选择一个'))
  95. } else {
  96. callback()
  97. }
  98. }
  99. //下拉框
  100. var select = (rule, value, callback) => {
  101. if (value.length < 1) {
  102. return callback(new Error('请选择一个'))
  103. } else {
  104. callback()
  105. }
  106. }
  107. //QQ号码验证
  108. var QQV = (rule, value, callback) => {
  109. if (value === '') {
  110. callback(new Error('输入QQ号'))
  111. } else if (/^[1-9][0-9]{4,10}$/.test(value)) {
  112. callback()
  113. } else {
  114. callback(new Error('输入正确的QQ号'))
  115. }
  116. }
  117. // 类似金钱,首位不为0,最多2位小数
  118. function checkNumPot2 (rule, value, callback) {
  119. const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  120. if (!value) {
  121. return callback(new Error('请填写数字'))
  122. } else if (!reg.test(value)) {
  123. return callback(new Error('请填写数字,最多2位小数'))
  124. } else {
  125. callback()
  126. }
  127. }
  128. // 身份证
  129. function checkIdNum (rule, value, callback) {
  130. const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  131. if (!value) {
  132. return callback(new Error('证件号码不能为空'))
  133. } else if (!reg.test(value)) {
  134. return callback(new Error('证件号码不正确'))
  135. } else {
  136. callback()
  137. }
  138. }
  139. // 整数
  140. function checkInterNum (rule, value, callback) {
  141. const reg = /^[0-9]*[1-9][0-9]*$/
  142. if (!value) {
  143. return callback(new Error('请填写整数'))
  144. } else if (!reg.test(value)) {
  145. return callback(new Error('请输入整数'))
  146. } else {
  147. callback()
  148. }
  149. }
  150. const confirmPasswordValidator = (rule, value, callback, password) => {
  151. if (password !== value) {
  152. callback(new Error('两次密码输入不一致'))
  153. } else {
  154. callback()
  155. }
  156. }
  157. const formRules = {
  158. QQ: [{ required: true, validator: QQV, trigger: 'blur' }],
  159. mobile: [{ required: true, pattern: /^1[23456789]\d{9}$/, message: '目前只支持中国大陆的手机号码', trigger: 'blur' }],
  160. password: [{ required: true, min: 6, max: 18, message: '长度必须大于6位长度在 6 到 18 个字符之间', trigger: 'blur' }],
  161. confirmPassword: (form) => {
  162. return [
  163. { required: true, min: 6, max: 18, message: '长度必须大于6位长度在 6 到 18 个字符之间', trigger: 'blur' },
  164. {
  165. required: true,
  166. validator: (rule, value, callback) => confirmPasswordValidator(rule, value, callback, form.password),
  167. trigger: 'blur'
  168. }
  169. ]
  170. },
  171. required: [{ required: true, min: 1, message: '必填', trigger: 'blur' }],
  172. numPot2: [{ required: true, validator: checkNumPot2, trigger: 'blur' }],
  173. InterNum: [{ required: true, validator: checkInterNum, trigger: 'blur' }],
  174. coins: [{ required: true, validator: checkInterNum, trigger: 'blur' }]
  175. }
  176. Vue.prototype.formRules = formRules