jQuery support “:invalid” selector

Using querySelectorAll as suggested by @JanDvorak (and his answer should be accepted for thinking of that), you can write your own expression, making .is(':invalid') valid ?

jQuery.extend(jQuery.expr[':'], {
    invalid : function(elem, index, match){
        var invalids = document.querySelectorAll(':invalid'),
            result = false,
            len = invalids.length;

        if (len) {
            for (var i=0; i<len; i++) {
                if (elem === invalids[i]) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
});

now you can do :

if( $(e.target).is(':invalid') ){ ... }

FIDDLE

Leave a Comment