4

I'm building a fairly simple plugin with jQuery that validates a form. When an element is assessed to be valid I run

$(this).prop("valid", false);

In certain instances I'm going to want to block the form from submitting if anything is invalid. Essentially I'm looking for a way to select all the elements where the valid property is false? Am I going to have to iterate over all relevant elements and check for a false reading or is there a jQuery selector that I've overlooked? Something like:

$("input[valid=false]");

Would be nice. Any suggestions?

Dormouse
  • 5,130
  • 1
  • 26
  • 42
  • keep in mind that 1.6 treats properties and attributes very differently. Properties are defined on first load. So instead of using `prop` you should use `attr` if you want to change values. Your selector is correct. Just quote the `false` bit – JohnP May 10 '11 at 10:19
  • @JohnP Is jQuery clever enough to work with `false` instead of `"false"` there? Maybe I stuffed something up, but it [didn't work for me](http://jsfiddle.net/QQsvJ/). – alex May 10 '11 at 10:28
  • @alex haha well we'd expect it to. But I've answered a couple of questions where adding the quotes fixed the issue. Better safe than sorry! – JohnP May 10 '11 at 10:30
  • @JohnP Check out the fiddle... did I do something wrong? – alex May 10 '11 at 10:31
  • @alex I'm guessing that that the `[attr='value']` filter does not pick up properties. 1.6 has drawn the line between properties and attributes. When I changed the code to use `attr()`, calling `.attr("a", true)` translates to `[a="a"]`. So looks like booleans are out ^_^ – JohnP May 10 '11 at 10:44
  • Reading it again, my first comment is completely the other way around >. – JohnP May 10 '11 at 10:50

3 Answers3

5

Try this...

$('input').filter(function() {
   return this.valid === false; 
});

It will return all input elements (do you mean :input?) where its valid property is false (notice strict equality comparison).

alex
  • 479,566
  • 201
  • 878
  • 984
  • Sorry I actually tried to but I had to wait a few minutes and then got waylaid. Thanks again for the answer – Dormouse May 10 '11 at 16:20
5

Reusable :invalid jQuery selector filter

You could write a simple selector filter:

$.extend($.exp[":"], {
    invalid: function(element) {
        return element.valid === false;
    }
});

Then simply combine it with whatever selector your using to get your elements ie:

// all invalid inputs
$(":input:invalid");
// or all invalid text boxes
$("input[type=text]:invalid");

That's it.

Let's put it all in a simple plugin that you can easily include in your script code or put in a script file (for reusability purposes on several pages as well as several applications if you'd use the same validation functionality):

(function($) {

    $.extend($.exp[":"], {
        invalid: function(element) {
            return element.valid === false;
        }
    });

})(jQuery);
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0

use this:

$(':input').each( function() {
   return $(this).prop('valid');
});
一二三
  • 21,059
  • 11
  • 65
  • 74
xkeshav
  • 53,360
  • 44
  • 177
  • 245