vee-validate.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import Vue from 'vue'
  2. import VeeValidate, {Validator} from 'vee-validate'
  3. import zh from 'vee-validate/dist/locale/zh_CN'
  4. import VueI18n from 'vue-i18n'
  5. Vue.use(VueI18n)
  6. const i18n = new VueI18n({
  7. locale: 'zh_CN'
  8. })
  9. Vue.use(VeeValidate, {
  10. i18n,
  11. i18nRootKey: 'validation',
  12. dictionary: {
  13. zh
  14. }
  15. })
  16. // 配置中文
  17. // Validator.addLocale(zh)
  18. const config = {
  19. locale: 'zh_CN'
  20. }
  21. Vue.use(VeeValidate, config)
  22. // 自定义validate
  23. const dictionary = {
  24. zh_CN: {
  25. messages: {
  26. vipName: () => '用户名不能为空',
  27. required: (field) => '请输入' + field
  28. },
  29. attributes: {
  30. vipName: '会员名'
  31. }
  32. }
  33. }
  34. console.log('1', dictionary)
  35. console.log(Validator)
  36. Validator.localize('zh_CN', dictionary)
  37. // 自定义会员名验证
  38. Validator.extend('vipName', {
  39. getMessage: field => field + '会员名不能为空',
  40. validate: value => {
  41. return value.length === ''
  42. }
  43. })
  44. // 自定义手机验证
  45. Validator.extend('phone', {
  46. getMessage: field => field + '必须是11位手机号码',
  47. validate: value => {
  48. return value.length === 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)
  49. }
  50. })