P
A
B
C
A
S
Thoughts, links, pictures on music, food, wine, film, tech etc.

2009-09-29

I discovered this:

Checking blank form inputs with jQuery

I've been checking lots of form fields for a blank value (eg. they contain nothing, or just whitespace) lately, and I wanted to emulate Rails' String#blank? to check this in jQuery.

I had been doing something like this:

if($.trim($(this).val()) == '') ... 

But that's so ugly I felt there must be a better way. Of course, there is:

if($(this).is(':blank')) ...

Simple, clear, and jQuery-ish.

Update This technique, while awesome, isn't bundled with jQuery and requires the jQuery validation plugin (docs).


 
Made by Paul Campbell. paul@rushedsunlight.com. Twitter. Github.