JSFiddle - React, Tailwind, and code Playground

by Eric Hynds

HTML

<form id="foo">

</form>

JavaScript

// 100% untested theoretical code

function Listen() {

    var Validation = function(elem) {
        this.elem = elem;
        this.obj = elem.validate();
    };
    
    Validation.prototype = {
        cancel: function(){
            $.removeData(this.elem, "validator");
        },
        use: function(){
        }
    };

    this.for = function(selector, callback) {
        var elem = $(selector), args = [];
    
        if (elem.length) {
            if( elem.is("form") ){
                args.push( new Validation(elem) );
            }
            
            callback.apply(elem, args);
        }
    
        return this;
    };

    this.validation = function(){
    
    };
}

// usage:

// if selector is a form, handler receives a validation obj instance
Listen.for("#foo", function( validation ){

    // "this" context: #foo jQuery object
    
    // to kill automatic form validation
    validation.cancel();

    // use validation from another selector
    // would need a API like Listen.validationFor("#bar", {});

    validation.use("#bar");
});