Search code examples
javascriptjqueryvalidationbackbone.js

Backbone validation on keyup


I am using Backbone validation for validating some input fields, like so...

return Dialogs.Base.extend({
    template: 'container',
    validation: {
        '#input1': {
            required: true
        },
        '#input2': {
            translator: Translators.Date(),
            required: true
        },
        '#input3': {
            required: true
        }
...

These work fine but the required attribute is only satisfied when you click out of the selected input box. What if I want to validate the input as the user is typing?

Another issue is if I set the validation of one of the inputs using Jquery...

$("#input1").val(value);

This will not satisfy the required attribute, the validation treats the input as if it were empty even though it has a value.

Is there a way to use validation so that it catches any live change in some input?


Solution

  • What if I want to validate the input as the user is typing?

    You will need to add an event listener to the user typing, and call the isValid method:

    model.isValid()