Current location: Hot Scripts Forums » General Web Coding » JavaScript » Need help w Form validation for checkbox array


Need help w Form validation for checkbox array

Reply
  #1 (permalink)  
Old 07-15-08, 09:49 AM
Jooba Jooba is offline
New Member
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Need help w Form validation for checkbox array

I'm web designer w little experience in coding. Working on a form that has fields with check boxes to be checked. How can I include validation for these checkbox array in the validation_required function that I have for other areas?

Here is the code I have so far for the validations in the head for (onSubmit="return validate_form(this)" ) and the code for check boxes area in the body:
_______________________________
javascript Code:
  1. <script type="text/javascript">
  2. function validate_required(field,alerttxt)
  3. {
  4. with (field)
  5. {
  6. if (value==null||value=="")
  7.   {alert(alerttxt);return false}
  8. else {return true}
  9. }
  10. }function validate_form(thisform)
  11. {
  12. with (thisform)
  13. {
  14. if (validate_required(company_name,"Company name must be filled out!")==false)
  15.   {company_name.focus();return false}
  16. if (validate_required(address,"Company address must be filled out!")==false)
  17.   {address.focus();return false}
  18. if (validate_required(city,"City must be filled out!")==false)
  19.   {city.focus();return false}
  20. if (validate_required(state,"State must be filled out!")==false)
  21.   {state.focus();return false}
  22. if (validate_required(zip,"Zip code must be filled out!")==false)
  23.   {zip.focus();return false}
  24. if (validate_required(country,"Country name must be filled out!")==false)
  25.   {country.focus();return false}
  26. if (validate_required(company_phone,"Phone must be filled out!")==false)
  27.   {company_phone.focus();return false}
  28. if (validate_required(company_email,"Company email must be filled out!")==false)
  29.   {company_email.focus();return false}
  30. if (validate_required(tax_id_number,"Federal Tax ID Number must be filled out!")==false)
  31.   {tax_id_number.focus();return false}
  32.  
  33. }
  34. }
  35. </script>
________________________________________

This is the checkbox area in the body:
HTML Code:
<tr>
<td colspan="4" valign="top">
<input name="employees_count" type="checkbox" id="a" value="1-9" />1-9 
<input name="employees_count" type="checkbox" id="b" value="10-25" />10-25
<input name="employees_count" type="checkbox" id="c" value="26-50" />26-50
<input name="employees_count" type="checkbox" id="d" value="51-100" />51-100
<input name="employees_count" type="checkbox" id="e" value="101+" />101+
</td>
</tr>
____________________________________
Can someone help me with this?
Much thanks.

Last edited by Nico; 07-15-08 at 03:55 PM. Reason: Wrappers.
Reply With Quote
  #2 (permalink)  
Old 07-16-08, 04:45 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
What kind of validation do you need? Should only one box be checked? If so, I recommend using radio buttons instead.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #3 (permalink)  
Old 07-16-08, 04:53 PM
Jooba Jooba is offline
New Member
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I need to have option to check more than one box, that is why I want to use the checkboxes instead of radio buttons.
Jooba
Reply With Quote
  #4 (permalink)  
Old 07-16-08, 04:56 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, and you just want to check to make sure atleast one box is checked, right?
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #5 (permalink)  
Old 07-16-08, 04:58 PM
Jooba Jooba is offline
New Member
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Yes. That will do.
Jooba
Reply With Quote
  #6 (permalink)  
Old 07-16-08, 05:01 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:
JavaScript Code:
  1. <script type="text/javascript">
  2. function validate_required(field,alerttxt)
  3. {
  4.     with (field)
  5.     {
  6.     if (value==null||value=="")
  7.       {alert(alerttxt);return false}
  8.     else {return true}
  9.     }
  10. }
  11. function validate_form(thisform)
  12. {
  13.     with (thisform)
  14.     {
  15.     if (validate_required(company_name,"Company name must be filled out!")==false)
  16.       {company_name.focus();return false}
  17.     if (validate_required(address,"Company address must be filled out!")==false)
  18.       {address.focus();return false}
  19.     if (validate_required(city,"City must be filled out!")==false)
  20.       {city.focus();return false}
  21.     if (validate_required(state,"State must be filled out!")==false)
  22.       {state.focus();return false}
  23.     if (validate_required(zip,"Zip code must be filled out!")==false)
  24.       {zip.focus();return false}
  25.     if (validate_required(country,"Country name must be filled out!")==false)
  26.       {country.focus();return false}
  27.     if (validate_required(company_phone,"Phone must be filled out!")==false)
  28.       {company_phone.focus();return false}
  29.     if (validate_required(company_email,"Company email must be filled out!")==false)
  30.       {company_email.focus();return false}
  31.     if (validate_required(tax_id_number,"Federal Tax ID Number must be filled out!")==false)
  32.       {tax_id_number.focus();return false}
  33.     
  34.     }
  35.     var cBoxes=document.getElementsByName("employees_count");
  36.     var isChecked=false;
  37.     for(var i=0;i<cBoxes.length;i++)
  38.     {
  39.         if(cBoxes[i].checked){
  40.             isChecked=true;
  41.             break;
  42.         }
  43.     }
  44.     if(!isChecked){
  45.         return false;
  46.     }
  47. }
  48. </script>
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #7 (permalink)  
Old 07-18-08, 11:43 AM
Jooba Jooba is offline
New Member
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi TwoD,
just to let you know I tried your script but it still would not work correctly, so I changed the field to a List/Menu and added the code below to the script which worked. But anyway thanks for your help. Jooba
JavaScript Code:
  1. }
  2.    else if (value==null||value==""||value=="Select from list below"){
  3.     alert(alerttxt);
  4.     return false;
  5.        }
  6.    else {return true}
  7. _______________________________
  8.  
  9. if (validate_required(employees_count,"Number of employees must be selected from the list!")==false)
  10.   {employees_count.focus();return false}

Last edited by TwoD; 07-18-08 at 11:49 AM. Reason: Added JS wrappers
Reply With Quote
  #8 (permalink)  
Old 07-18-08, 11:51 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, what did not work? Any errors? Ctrl+Shift+J in FF to bring up the error console.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I cannot find what i messed up. Parse error: syntax error, unexpected T_CONSTANT_ENCA bilicek.com PHP 3 01-31-08 04:54 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Form Validation slimey PHP 10 03-20-06 06:23 PM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 11:40 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.