Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vee-validate how to set digit limit between two number?

I am trying to restrict the number of digits a user can input between 3-6.

For some reason, I can't find how to do that.

this is the code I have to force user to add three digits only

<input type="text"  name='account-field-3' v-validate="'required|digits:3'" placeholder="6" class="form-control" > 

But what I need is between 3-6.

like image 503
hidar Avatar asked Dec 08 '22 15:12

hidar


2 Answers

You need to use min and max

Max: https://baianat.github.io/vee-validate/guide/rules.html#max

Min: https://baianat.github.io/vee-validate/guide/rules.html#min

<input type="text"  name='account-field-3' v-validate="'required|min:3|max:6'" placeholder="6" class="form-control" > 
like image 134
Igor Martins Avatar answered Dec 11 '22 09:12

Igor Martins


Its been long but here is a solution which worked for me.

//Javascript File
import { min, max, numeric } from "vee-validate/dist/rules";
import { extend, validate, localize } from "vee-validate";
import en from "vee-validate/dist/locale/en.json";
localize({
    en
});

extend("min", min);
extend("max", max);
extend("numeric", numeric);

extend('digits_between', {
  async validate(value, { min, max }) {
        const res = await validate(value, `numeric|min:${min}|max:${max}`,)
            return res.valid;
  },
  params: ['min', 'max'],
  message: 'The {_field_} must be between {min} and {max} digits'
});

<!-- vue file -->
<ValidationProvider
  rules="digits_between:3,8"
  v-slot="{ errors }"
  name="Country"
>
  <input v-model="value" type="text">
  <span>{{ errors[0] }}</span>
</ValidationProvider>

like image 43
aasiph Avatar answered Dec 11 '22 09:12

aasiph