By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,441 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

Javascript validation of dependent form fields

rush it
P: 18
First off, let me say I'm pretty new to web design and programming in general. Call it a career change. But I love it. :)

I'm building an online form for users to apply for a discount on park sports for their child. In this form, I need to have one of the fields validate another. Field_1, a radio button, is in one section of the form but I can't validate it because it may not apply to all users. Field_2, a checkbox, is in the section below it, related to Field_1, and does require validation.

Field_1 is the 'Free Lunch' selection in an area that asks users which lunch program their child is in at school. (A criterion for receiving the park sports discount.)

Field_2 asks the user to agree that they understand that if their child is enrolled in the free lunch, they only pay 25% of the park sports fee. (I hope this is all making sense. I'm trying to break it down into sections.)

I would like to have the '25%' checkbox, if selected, prompt the user to check the 'Free Lunch' radio button. I know this can be done for matching values, but I just want to check for field selection.

This is what I have for a simple field.

Expand|Select|Wrap|Line Numbers
  1. var s=document.forms["form2"]["name"].value
  2.     if (s==null || s=="")
  3.   {
  4.   alert("Please enter Name");
  5.   return false;
  6.   }
  7.  

This works fine for the simple fields. I've been researching how to have Field_2 validate Field_1, and quite frankly, I'm very confused as to which way I should go.

This is something I came across, but it doesn't work, I think, because this function calls for names, not values, and I'm working with radio buttons and checkboxes. It simply doesn't not validate the field at all.

Expand|Select|Wrap|Line Numbers
  1. var tos = new Ext.form.checkbox({ 
  2.       name: 'free', 
  3.       fieldLabel: 'free' 
  4.      }); 
  5.   var lunch_program = new Ext.form.radio({ 
  6.       name: 'Free Lunch', 
  7.       fieldLabel: 'Free Lunch' 
  8.        validator: function(checked){ 
  9.     if(free.getValue() != checked) { 
  10.         return 'Please select 'Free Lunch Participant' under 'Does your child particpate in the school's free and reduced program?' ; 
  11.       } else {
  12.      return true; 
  13.     } 
  14.  
I'd appreciate any guidance anyone can suggest.
Oct 4 '11 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Are you currently using the Ext JS library or did you use it because you found this code?
Oct 13 '11 #2

rush it
P: 18
I was already using javascript when I found this code.
Oct 14 '11 #3

Rabbit
Expert Mod 10K+
P: 12,430
He's asking if you are using the EXT library on purpose. Because you can't just start using it out of nowhere.
Oct 14 '11 #4

rush it
P: 18
Well, as I said in my original post, I'm new to web design and pretty new to programming. I've not even heard of the Ext library before. I see the Ext in the code now. This code is just an example. If there's another way to do this in just javascript, IF that makes sense, that would be great. Right now, I'm hung up on the syntax. Thanks.
Oct 17 '11 #5

gits
Expert Mod 5K+
P: 5,390
to simplyfy that all - please post an example of your code with a description of the requirement, and don't post snippets that you don't use. so in short words: what exactly do you have so far?

regards,
gits
Oct 18 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.