A reader on my first post on jQuery Form Validation asked an interesting question - how do you validate select form fields? Specifically, given that a drop down may have a 'Other' option, how do mark a text field required if the drop down is set to Other?
Before I answer that, let me first point out a simpler example. Given a drop down where the first option is 'Pick one of the below', how do you require a user to select one of the other items? Easy! Just make the first option have a blank value. Consider:1 <select id="cjob" name="job">
2 <option value="">Select a Job</option>
3 <option value="1">Jedi</option>
4 <option value="2">Annoying Droid</option>
5 <option value="3">Bad Guy</option>
6 </select>
Notice how all the options, except the first, have a value? By just setting this drop down to required in our rules block, jQuery will handle ensuring that the user doesn't leave the drop down on the first option. To be honest, even with all the respect I've gained for jQuery, I didn't think it would be that easy. You can even specify that the user select a certain number of options (for multi-select drop downs) or that they must select a certain number but no more than some max. The plugin author has a nice demo page with examples of that. What I didn't see, though, was an example that matched what the reader wanted. If the drop down is on 'other', make some other field required. Here is how I solved it.
First, our form:
2 <option value="">Select a Job</option>
3 <option value="1">Jedi</option>
4 <option value="2">Annoying Droid</option>
5 <option value="3">Bad Guy</option>
6 </select>
1 <form id="commentForm" method="get" action="">
2 <fieldset>
3 <legend>A simple comment form with submit validation and default messages</legend>
4 <p>
5 <label for="cname">Name</label>
6 <em>*</em><input id="cname" name="name" size="25" />
7 </p>
8 <p>
9 <label for="cjob">Job</label>
10 <em>*</em><select id="cjob" name="job">
11 <option value="">Select a Job</option>
12 <option value="1">Jedi</option>
13 <option value="2">Annoying Droid</option>
14 <option value="3">Bad Guy</option>
15 <option value="other">Other (Enter Below)</option>
16 </select>
17 </p>
18 <p>
19 <label for="cother">Other Job:</label>
20 <input id="cother" name="otherjob" size="25" />
21 </p>
22 <p>
23 <input class="submit" type="submit" value="Submit"/>
24 </p>
25 </fieldset>
26 </form>
The form has 3 main fields: Name, Job, and Other Job. I want name to be required. I want job to be required. If you select Other for a job, I then want Other Job to be required as well. Here is the rules block I used:
2 <fieldset>
3 <legend>A simple comment form with submit validation and default messages</legend>
4 <p>
5 <label for="cname">Name</label>
6 <em>*</em><input id="cname" name="name" size="25" />
7 </p>
8 <p>
9 <label for="cjob">Job</label>
10 <em>*</em><select id="cjob" name="job">
11 <option value="">Select a Job</option>
12 <option value="1">Jedi</option>
13 <option value="2">Annoying Droid</option>
14 <option value="3">Bad Guy</option>
15 <option value="other">Other (Enter Below)</option>
16 </select>
17 </p>
18 <p>
19 <label for="cother">Other Job:</label>
20 <input id="cother" name="otherjob" size="25" />
21 </p>
22 <p>
23 <input class="submit" type="submit" value="Submit"/>
24 </p>
25 </fieldset>
26 </form>
1 rules: {
2 name: {
3 required: true,
4 minlength: 2
5 }
6 ,job: {
7 required: true
8 }
9 ,otherjob: {
10 required: function(element) {
11 return $("#cjob").val() == 'other'
12 }
13 }
14 }
Let's skip the first rule since it isn't anything new. The second rule applies just to the drop down. The use of required here will ensure that an option with some value has been picked. If I leave it on the first option there will be no value and the rule will fail. The third rule is where things get interesting. I essentially provide an inline function that returns true or false. The field will be required if and only if the value of cjob (my drop down) is set to other.
Next, I ensured my messages made all of the above clear:
2 name: {
3 required: true,
4 minlength: 2
5 }
6 ,job: {
7 required: true
8 }
9 ,otherjob: {
10 required: function(element) {
11 return $("#cjob").val() == 'other'
12 }
13 }
14 }
1 messages: {
2 name: {
3 required: "Stand up for your comments or go home.",
4 minlength: jQuery.format("You need to use at least {0} characters for your name.")
5 }
6 ,job: "You must select a job."
7 ,otherjob: "If you select 'other' for a job, you must enter it."
8 }
Pretty simple, right? Can anyone recommend an alternate way of solving this? You can find a demo of this code here:
http://www.coldfusionjedi.com/demos/jqueryvalidation/test4.html
p.s. I think I may forgo Star Wars for my next tattoo and just do 'jQuery FTW'. Or is that too fan boy?
2 name: {
3 required: "Stand up for your comments or go home.",
4 minlength: jQuery.format("You need to use at least {0} characters for your name.")
5 }
6 ,job: "You must select a job."
7 ,otherjob: "If you select 'other' for a job, you must enter it."
8 }
Comment 1 written by Sebastiaan on 16 February 2009, at 8:50 AM
Comment 2 written by salvatore fusto on 16 February 2009, at 9:14 AM
may be i've not understood requirements; if you set the job select as required, form is validated only if you select a job: your test4.html works this way.
but i think that your reader wants that jobs select in not a required field, but he wants that, if no job is selected, other job field became required: to do so you have not to validate job field, and validate other job field as you have done.
do you agree?
regards
salvatore
Comment 3 written by Raymond Camden on 16 February 2009, at 9:15 AM
Comment 4 written by salvatore fusto on 16 February 2009, at 9:30 AM
first of all, excuse my poor English.
What i mean is:
1)The "Name" field is rquired;
2)The "Other job" field is required if, and only if, user does not select any job from this select
so the rules are:
rules: {
name: {
required: true,
minlength: 2
}
,otherjob: {
required: function(element) {
return $("#cjob").val() == 'other'
}
}
}
are these the required rules ?
regards
salvatore
Comment 5 written by Raymond Camden on 16 February 2009, at 9:33 AM
Comment 6 written by Jörn Zaefferer on 16 February 2009, at 10:26 AM
Comment 7 written by Raymond Camden on 16 February 2009, at 10:29 AM
Comment 8 written by Chris on 17 February 2009, at 3:43 PM
Comment 9 written by Patrick Flynn on 26 February 2009, at 12:50 PM
Comment 10 written by Raymond Camden on 26 February 2009, at 2:53 PM
jQuery.validator.messages.required = "*";
That would set the message for required. You would need to repeat it for email, url, etc.
Comment 11 written by Byron Raines on 3 March 2009, at 10:04 PM
Enjoying these write-ups. I've have the select validation working from your example. If the "other" field is left blank as per the validation rules, it shows my error message, however the select box reverts back to (in my case) "Choose One". So I have to reselect "Other". Is this the correct behavior. I can't seem to find anything about this in the docs.
Thanks
byron
Comment 12 written by Raymond Camden on 3 March 2009, at 10:08 PM
Comment 13 written by Byron Raines on 3 March 2009, at 11:13 PM
Anywho, thanks for getting back to me, and again for all your write-ups. They are informative and helpful.
jQuery is very cool.
Comment 14 written by Byron Raines on 3 March 2009, at 11:24 PM
var1: yes/no var1comment: text
var2: yes/no var2comment: text
on so
I know how to enable and validate the comment box. Do I have to have a separate $('var1'), $('var2') for each one (I have many), or is there someway to consolidate that. I was think there maybe someway using a "for each" type method, but not sure it works that way.
I guess a could do it with "regular" javascript functions with onClick on the <input>, but would like to try it with jQuery.
Maybe a write up if it can be done??
Thanks again
Byron
Comment 15 written by Raymond Camden on 4 March 2009, at 9:16 AM
Comment 16 written by Byron Raines on 4 March 2009, at 2:22 PM
Byron
Comment 17 written by Mark on 15 April 2009, at 7:28 AM
Thanks for the tutorial, its helped me a lot! I just want to know how to remove on submit validation. i just need on blur and key up.
Thanks in advance
Comment 18 written by Mark on 15 April 2009, at 7:39 AM
onsubmit:false does the trick
Comment 19 written by vandy on 8 June 2009, at 12:33 PM
I have a user control on a page. In user control some fields have validation. I did the validations by using class properties, but I want to call some method on onsubmit of form. Since the validation occurs on onsubmit event of form, I am scared that the method I want to call may not be called. I can use submit behavior at the form level but I dont know what to do at the control level, I mean how to add methods to form's submit event from a control if we are using for validation of jquery.
Comment 20 written by Gustavo on 11 August 2009, at 12:17 AM
Comment 21 written by Kevin on 22 September 2009, at 2:59 PM
http://psylicyde.com/misc/jquery-validate/demo/tes...
Comment 22 written by Aloel on 17 November 2009, at 11:34 AM
Comment 23 written by Raymond Camden on 17 November 2009, at 12:50 PM
Comment 24 written by Aloel on 18 November 2009, at 7:56 AM
[Add Comment] [Subscribe to Comments]