form.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Validate v1.0.0 通用验证
  3. * @description 项目中用到的表单验证规则
  4. */
  5. import test from '@/sheep/helper/test.js';
  6. // 用户名
  7. export const username = {
  8. rules: [
  9. {
  10. required: true,
  11. errorMessage: '请输入用户名',
  12. },
  13. {
  14. validateFunction: function (rule, value, data, callback) {
  15. if (!value) {
  16. callback('用户名不能为空');
  17. }
  18. return true;
  19. },
  20. },
  21. ],
  22. };
  23. // 手机号
  24. export const mobile = {
  25. rules: [
  26. {
  27. required: true,
  28. errorMessage: '请输入手机号',
  29. },
  30. {
  31. validateFunction: function (rule, value, data, callback) {
  32. if (!test.mobile(value)) {
  33. callback('手机号码格式不正确');
  34. }
  35. return true;
  36. },
  37. },
  38. ],
  39. };
  40. // 密码
  41. export const password = {
  42. rules: [
  43. {
  44. required: true,
  45. errorMessage: '请输入密码',
  46. },
  47. {
  48. validateFunction: function (rule, value, data, callback) {
  49. if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/.test(value)) {
  50. callback('需包含字母和数字,长度在6-12之间');
  51. }
  52. return true;
  53. },
  54. },
  55. ],
  56. };
  57. // 短信验证码
  58. export const code = {
  59. rules: [
  60. {
  61. required: true,
  62. errorMessage: '请输入验证码',
  63. },
  64. ],
  65. };
  66. // 真实姓名
  67. export const realName = {
  68. rules: [
  69. {
  70. required: true,
  71. errorMessage: '请输入姓名',
  72. },
  73. {
  74. validateFunction: function (rule, value, data, callback) {
  75. if (!test.chinese(value)) {
  76. callback('请输入汉字');
  77. }
  78. return true;
  79. },
  80. },
  81. ],
  82. };
  83. export const taxName = {
  84. rules: [
  85. {
  86. required: true,
  87. errorMessage: '请输入发票抬头名称',
  88. },
  89. {
  90. validateFunction: function (rule, value, data, callback) {
  91. if (!test.chinese(value)) {
  92. callback('请输入汉字');
  93. }
  94. return true;
  95. },
  96. },
  97. ],
  98. };
  99. // 税号
  100. export const taxNo = {
  101. rules: [
  102. {
  103. required: true,
  104. errorMessage: '请输入税号',
  105. },
  106. ],
  107. };
  108. // 开户行
  109. export const bankName = {
  110. rules: [
  111. {
  112. required: true,
  113. errorMessage: '请输入开户行',
  114. },
  115. {
  116. validateFunction: function (rule, value, data, callback) {
  117. if (!test.chinese(value)) {
  118. callback('请输入汉字');
  119. }
  120. return true;
  121. },
  122. },
  123. ],
  124. };
  125. // 银行卡号
  126. export const bankCode = {
  127. rules: [
  128. {
  129. required: true,
  130. errorMessage: '请输入银行卡号',
  131. },
  132. {
  133. validateFunction: function (rule, value, data, callback) {
  134. if (!/^\d+$/.test(value) ) {
  135. callback('请输入正确的银行卡号');
  136. }
  137. return true;
  138. },
  139. },
  140. ],
  141. };
  142. // 支付宝账号
  143. export const alipayAccount = {
  144. rules: [
  145. {
  146. required: true,
  147. errorMessage: '请输入支付宝账号',
  148. },
  149. {
  150. validateFunction: function (rule, value, data, callback) {
  151. let isEmail = test.email(value);
  152. let isMobile = test.mobile(value);
  153. if (!isEmail && !isMobile) {
  154. callback('请输入正确账号');
  155. }
  156. return true;
  157. },
  158. },
  159. ],
  160. };
  161. // 支付宝账号
  162. export const email = {
  163. rules: [
  164. {
  165. required: true,
  166. errorMessage: '请输入邮箱',
  167. },
  168. {
  169. validateFunction: function (rule, value, data, callback) {
  170. let isEmail = test.email(value);
  171. if (!isEmail) {
  172. callback('请输入正确邮箱账号');
  173. }
  174. return true;
  175. },
  176. },
  177. ],
  178. };
  179. export default {
  180. mobile,
  181. alipayAccount,
  182. bankCode,
  183. bankName,
  184. realName,
  185. password,
  186. code,
  187. taxNo,
  188. taxName,
  189. email
  190. };