如何在Vuelidate中进行自定义验证

时间:2018-12-26 07:17:20

标签: vuelidate

我正在尝试进行自定义验证,以检查客户名称。客户名称只能是字符和空格。这是我尝试过的:

<v-text-field 
   outline 
   label="Customer Name" 
   :counter="30"
   maxLength="30"
   :error-messages="fieldErrors('enquiryForm.customername')" 
   v-model="enquiryForm.customername"
   @input="$v.enquiryForm.customername.$touch()"
   @blur="$v.enquiryForm.customername.$touch()">
</v-text-field>

import { required, maxLength } from 'vuelidate/lib/validators'


export default {

mixins: [ validationMixin ],

validations: {
    enquiryForm: {

        customername: { required, maxLength: maxLength(30), validName }
},

validationMessages: {
    enquiryForm: {

        customername: {
            required: 'Please enter Customer name',
            maxLength: 'Maximum length should be 30 characters',
            validName: 'Name must be alphabets and space'
        }
},

methods: {

    validName (name) {
      var re = /^[a-zA-Z ]*$/;
      return re.test(name);
    }
}

我已经在Google中搜索,但找不到符合我要求的任何内容。

0 个答案:

没有答案