Related Posts

Archive

Conditional Requirement in jquery.validate

December 21st, 2007

So, there is this form.

There always is a form.

I’m supposed to take the dimensions of a box, barrel, or somthing else - ‘other’ - and calculate the volume. Ideally, I’d like to require Jörn Zaefferer jQuery Plugin to to this - as of writing, it’s at version 1.1.2.

For a box, I’ll need height, width and depth.
For ‘other’, I’ll need the same.
For a barrel, width and depth would be redundant.

jquery.validate makes the required part of this simple.
<input name="height" id="height" class="{required:true}" />
<input name="width" id="width" class="{required:true}" />

Then comes the condition. Since one can specify a jQuery selector, there are two options (supposedly): by inclusion, and by exclusion. Inclusion is pretty easy (pardon my IDs):

<input name="depth" id="depth" class="{required:'#boxenOther:checked, #boxenBox:checked'}" />

Exclusion is more complicated - but more accurate.

<input name="depth" id="depth" class="{required:':input[@name=boxen]:not([value=barrel]):checked’}” />

As I have been lead to believe there will be no more options in the list, either would work. But as the latter can be read without looking somwhere else on the page, I’m using it.

-Sud.

Posted in JavaScript |

Comments are closed.

Previous post: Note to self [Ubuntu 7.10 LAMP setup]

Next Post: Booting my “New” Cobalt Qube 3 - part one