0

I have an input field and date schema that I need to validate (dd.mm.yy). I can not use a regular pattern for input, because it should not be allowed users to type letters, they need to type 22 and if the third one is not a dot, the type should be prevented.

The front is written in vue.js so I have

<input v-on:change="changeFunction" :sr-only="true" placeholder="dd.mm.yy"></input>

and then I tried to use functions like this

validate (e) {
      let number = new RegExp('(0[1-9]|[12][0-9]|3[01])[. /.](0[1-9]|1[012])[. /.]')
      let validate = number.test(e)
      if (validate) { return true }
}

and then to validate

changeFunction ({name, value}) {
      
      if (this.validate(value)) {
        do something
      } else {
        do something else
      }
    }

I get true and false once the type is complete, but I can type anything inside the input, not just numbers and dots. So is there any way that I can check RegExp while I am typing number by number and to prevent type if RegExp is not matched

Thanks

Marko
  • 1
  • 1
  • I think you need to be feeding the result of the validation into a model on the field. – Wayne Smallman Mar 09 '22 at 13:48
  • the strategy to prevent a user to feed a form input requires an event handler on keypress on such control and use event.preventDefault() when the condition to block user input is met. Here there's an example: https://stackoverflow.com/questions/895659/how-do-i-block-or-restrict-special-characters-from-input-fields-with-jquery – Diego D Mar 09 '22 at 14:08

0 Answers0