Skip to content Skip to sidebar Skip to footer

Use Jquery Validation Plugin To Add Real-time Validations To My Form

I want to validate my form in real-time (on input) using this jquery plugin : https://jqueryvalidation.org/rules/? This is an example of my current validation function: (function(n

Solution 1:

By default, it validates on the keyup event. However, validation is "lazy", not "eager", which means that no validation happens until after the first click of submit. So you'll have to tweak some settings.

$form.validate({
    rules : {
        // rules
    },
    onfocusout: function(element) {
        this.element(element); // triggers validation
    },
    onkeyup: function(element, event) {
        this.element(element); // triggers validation
    }
});

Your code:

required: function (element) {
    if ($(element).is(":visible")) {
        returntrue;
    }
    returnfalse;
}

You do not need to test for visibility. By default, the plugin will dynamically ignore any hidden field. Just set required to true and let the rest happen.

Post a Comment for "Use Jquery Validation Plugin To Add Real-time Validations To My Form"