form.js 3.8 KB

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