1

I am trying to create a regular expression in Vuelidate and add it to a form with several rules. The documentation does not say exactly how to use some custom rules, and I am using Composition API.

I have a field that I need to validate with a regex.

'^([A-ZÑ\x26]{3,4}([0-9]{2})(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1]))((-)?([A-Z\d]{3}))?$'

The field is inside a form, so I create the rules to be used later as follows.

// const rfc = helpers.regex('^([A-ZÑ\x26]{3,4}([0-9]{2})(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1]))((-)?([A-Z\d]{3}))?$')

I create the rules in this way

`const companyRules = {
  company: {
    RFC: { required, rfc },
    name: { required },
    line: { required },
    email: { required, email },
    phone: { required },
  },
  ...
};`

And finally I use them with the form

const v$ = useVuelidate(rules, form);

But it doesn't work, as it marks me as never being valid. I tried with other simpler regex but it still never works. How should I add the rule or what should I modify in it?

2 Answers 2

1

I'm experiencing the same issue using the regex helper. Even with a simple regex. I'm using the composition API + vue3 and a couple of decorators for translation.

// validators.ts
export const foo = withI18nMessage(
    helpers.regex("foo", /[a-z]*/)
);

// myform.vue
import { foo } from "validators"
const validators = {
    bar: {
        foo,
    }
}

Bar will remain invalid even with a valid value... For now I decided to not use the regex helper and use string.match instead.

export const sshPublicKey = withI18nMessage((value: string) =>
    Boolean(value.match(/[a-z]*/))
);
1
  • Thanks! Worked for me. Vuelidates regex 'helper' isn't as helpful as your answer Commented Jan 15 at 23:09
0

const companyRules = {
  company: {
    RFC: { required, rfc: helpers.regex('^([A-ZÑ\x26]{3,4}([0-9]{2})(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1]))((-)?([A-Z\d]{3}))?$') },
    name: { required },
    line: { required },
    email: { required, email },
    phone: { required },
  },
  ...
};


const v$ = useVuelidate(companyRules, form);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.