0
votes

I'm trying to validate a date of birth field on Vue.js to only allow dates before today, but I am unsure of how to implement Javascript to the before attributes or the date_between attributes

I'm trying to do something along the lines of

v-validate="'date_format:DD-MM-YYYY|before:changeDateFormat(new Date(Date.now()))'"

where changeDateFormat() is

changeDateFormat(dateStr) {
    if (dateStr != null) {
      var date = new Date(dateStr)
      var newDate = ("00"+(date.getDate())).slice(-2)+'-'+("00"+(date.getMonth()+1)).slice(-2)+'-'+date.getFullYear()
      return newDate
    }
    return ''

I'm quite new to Vue.js so I apologise if this is a simple question.

1

1 Answers

0
votes

Try to use rules expression like object.

VeeValidate syntax: https://baianat.github.io/vee-validate/guide/syntax.html

For example:

data: {
    validators: {
        BirthDate: {
            required: true,
            date_format: 'dd-MM-yyyy',
            before: this.changeDateFormat(new Date(Date.now()))
        }
    }
},
methods: {
   changeDateFormat(dateStr) {
      if (dateStr != null) {
        var date = new Date(dateStr)
        var newDate = ("00" + (date.getDate())).slice(-2) + '-' + ("00" + (date.getMonth() + 1)).slice(-2) + '-' +date.getFullYear()
        return newDate
      }
    return ''
   }
},

And your directive:

v-validate="validators.BirthDate"