Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you validate a checkbox in a form using OnSubmit?

Status
Not open for further replies.

Joe2k

Programmer
Jul 17, 2000
14
CA
Hi,<br><br>My problem is that I need a script that will make sure at least 1 of the 6 checkboxes are selected before being sent.&nbsp;&nbsp;This is the top part of the form, and I have all the other validations working except for the checkboxes.&nbsp;&nbsp;Here is what I have so far:<br><br>ONSUBMIT=&quot;<br>if (this.councillor1.checked == FALSE)<br>{<br>return true<br>}<br>else if (this.councillor2.checked == FALSE)<br>{<br>return true<br>}<br>else if (this.councillor3.checked == FALSE)<br>{<br>return true<br>}<br>else if (this.councillor4.checked == FALSE)<br>{<br>return true<br>}<br>else if (this.councillor5.checked == FALSE)<br>{<br>return true<br>}<br>else if (this.councillor6.checked == FALSE)<br>{<br>alert('Please select a councillor')<br>return false<br>} <br>else if (this.Subject.value == '')<br>{<br>alert('Please enter a title for the subject')<br>return false<br>}<br>etc.....<br><br>So what I need is it to read down the script and if does not read a selected checkbox, a alert message pops up saying that they need to make a selection.<br><br>Thanks a ton,<br><br>Joe<br>
 
Joe2k,&nbsp;&nbsp;Thanks, though that puts me right back where I started before I posted:<br><br>function OnSubmitCity(form){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;&nbsp;(form.Keywords.value==&quot;&quot; ¦¦ CITY.checked==false)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;You did not select any Cities and/or Keywords!\n\nPlease try again . . .&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>This gives an alert if there is no Keyword but it won't search by checkbox only.&nbsp;&nbsp;In other words, I must enter a Keyword so it is entirely ignoring the CITY.checked==false and I'm not sure how to word it properly.&nbsp;&nbsp;(I also tried document.CITY.checked==false and form.CITY.checked==false)<br><br>By the way, in case it helps to see what I am trying to do, the site is at: <A HREF=" TARGET="_new"> (Search I&R Database button, then select Search City. The online version has the workaround in the VBScript code rather than in the JavaScript)<br><br>Don
 
Don,<br><br>So let me get this straight you want validation for a list of checkboxes and a text field?&nbsp;&nbsp;Cause they need individual validation scripts.&nbsp;&nbsp;
 
Joe2k, I'm not sure exactly what you mean so let me try to explain it again. The form has a series of checkboxes, one for each city, all with the same name value.&nbsp;&nbsp;It also has a Keyword text search area. I want people to be able to select a city or cities to find what is listed in the database for those cities.&nbsp;&nbsp;OR, they can select cities and enter Keywords, which will show only the items matching the keywords in the selected cities.&nbsp;&nbsp;(They should also be able to enter a Keyword(s) by itself and search the whole database for all cities.)&nbsp;&nbsp;That part is VBscript and is online and working just fine and has multiple sql statements to handle the different types of searches.<br><br>What I want is a simple JavaScript alert that pops up when no city and/or no keyword is entered.&nbsp;&nbsp;In other words, they need to select or enter something for the search form to go to the VBscript.&nbsp;&nbsp;Right now, it is requiring a keyword whether or not a city is selected and that's not what I want.&nbsp;&nbsp;If there is an alert, the form should stay there (return false;), rather than going to the search script.<br><br>In case it makes a difference, the JavaScript is a separate file that is being called from a script src tag.<br><br>I hope this helps, and thanks!<br><br>Don
 
Ok here is what I did with my validation script.&nbsp;&nbsp;I had a list of checkboxes and mulitple text field.&nbsp;&nbsp;So the script I need was to make sure that they had at least on checkbox clicked and some text in the text field.&nbsp;&nbsp;But my the sounds of it you want it so that they can do one or the other.&nbsp;&nbsp;Cause if you want it so that they have to have some text in the text field I can help you no problem.&nbsp;&nbsp;But you want it so that say I typed Oakland in the &quot;keywords&quot; field and left the checkboxes untouched it would work properly.&nbsp;&nbsp;Then same goes for the checkboxes, like I click on checkbox then leave the text field blank, that what you want?
 
Joe2k,<br><br>Yes, I want one or the other (or both), which is why I tried to make my &quot;if&quot; statement look at either value with an &quot;or&quot; in between.&nbsp;&nbsp;My local copy (the one that is not online yet) is already working with an alert to require a keyword when using the code in my earlier posting, but it is not allowing a City to be selected by itself.&nbsp;&nbsp;That is, I get the alert if I do NOT select a city when there is no keyword and I don't want that.<br><br>The way our database search is set up, entering the city name as a Keyword won't necessarily get results unless the city name happens to be part of the agency name.&nbsp;&nbsp;&nbsp;Selecting the city checkbox however, looks at the database &quot;city&quot; field.&nbsp;&nbsp;In other words, the city and the keywords look at different fields in the database table and there are sql statements for different combinations. That's why the name=CITY is so important.<br><br>The site actually has four search pages but two are working just fine, and fixing this one will also fix the last one because it uses the same name=CITY tag and the same sql statements, though it searches on multiple cities with one checkbox.<br><br>Don
 
Well this script will make sure that a checkbox and a text field will be valid:<br><br>if (this.Subject.value == '')<br>{<br>alert('Please enter a title for the subject')<br>return false<br>}<br>else if (this.Message.value == '')<br>{<br>alert('Please enter a message')<br>return false<br>}<br>if ( MayorCouncil.checked ¦¦ <br> Mayor.checked ¦¦ <br> Councillor1.checked ¦¦ <br> Councillor2.checked ¦¦ <br> Councillor3.checked ¦¦ <br> Councillor4.checked)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;return true<br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>{<br>alert('Please select a councillor')<br>return false<br>}<br><br>So this script will make sure two text field must have some typing in it and that at least on of the checkboxes are selected.&nbsp;&nbsp;These are all required with my form.&nbsp;&nbsp;That is why they all have to be valid before being submitted.&nbsp;&nbsp;\<br><br>Hope this helps<br><br>Joe
 
Joe2k,<br><br>Was I supposed to leave the onClick=&quot;city=1&quot; onClick=&quot;city=2&quot; etc. in the form tags? With them there, and when I don't enter anything or select a city, I get the alert.&nbsp;&nbsp;But if I select a city, I get a runtime error that says, &quot;Object doesn't support this property or method&quot; Error &quot;CITY&quot;&nbsp;&nbsp;Going to the debugger has the HTML &quot;city=3&quot; (or which ever one was selected&quot; highlighted in yellow.<br><br>This is what I have now, though I think as you said, this will require BOTH city and Keywords once it's working correctly.&nbsp;&nbsp;I need it to work with either one but not necessarily both and, even though I have only one text area so needed to remove one from your code, I tried to keep it true to your idea just to get it working.&nbsp;&nbsp;Also, my system does not seem to like the single quotes, and I am not sure about semi-colons because your code didn't have any.&nbsp;&nbsp;Also, I couldn't get it to work at all with the this.Keywords.value etc. so I substitited form.Keywords.value:<br><br>function OnSubmitCity(form){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (form.Keywords.value == &quot;&quot;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Please enter Keywords&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>Don
 
Hey Don,<br><br>Ok I think I know what is wrong.&nbsp;&nbsp;K first on the onClick function is in the wrong place.&nbsp;&nbsp;It should be here:<br><br>&lt;input type=&quot;checkbox&quot; name=&quot;Councillor2&quot; value=&quot;JMarvinHunt&quot; onClick=&quot;<br>&nbsp;&nbsp;if (MayorCouncil.checked) <br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;alert('To select Councillor J. Marvin Hunt, please de-select Mayor &amp; Council')<br>&nbsp;&nbsp;return false<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;else (Councillor2.checked)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;return true<br>&nbsp;&nbsp;}&quot;&gt;<br><br>So your onClick should just be inside the checkbox properties.&nbsp;&nbsp;Not in your validation script.&nbsp;&nbsp;Ok now the validation script should go here:<br><br>&lt;form METHOD=&quot;POST&quot; ONSUBMIT=&quot;<br>&nbsp;&nbsp;if (this.Subject.value == '')<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;alert('Please enter a title for the subject')<br>&nbsp;&nbsp;return false<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;if ( MayorCouncil.checked ¦¦ <br> Mayor.checked ¦¦ <br> Councillor1.checked ¦¦ <br> Councillor2.checked ¦¦ <br> Councillor3.checked ¦¦ <br> Councillor4.checked)<br>&nbsp;&nbsp;{<br> return true<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;alert('Please select a councillor')<br>&nbsp;&nbsp;return false<br>&nbsp;&nbsp;} <br> <br>&nbsp;&nbsp;name=&quot;ContactCouncil&quot;&gt;<br><br>So to recap, the onClick should go inside the checkbox tag and the validation script should go in the &lt;form&gt; tag.&nbsp;&nbsp;Does this help?<br><br>Joe
 
Joe2k,<br><br>Thanks.&nbsp;&nbsp;In that case, I may not be able to use this method because we don't want the code visible on the html pages, except for onClicks etc.&nbsp;&nbsp;That's why I am using a separate file for the code, which contains other code as well.&nbsp;&nbsp;Can you not think of a way it could be done without the JavaScript inside the HTML?<br><br>Don
 
Don,<br><br>I don't know of a way for it to be done outside of JavaScript.&nbsp;&nbsp;Sorry and I know what if feels like to get a job like this done.&nbsp;&nbsp;When I got mine to work I was just playing around and tried something and got it to work.&nbsp;&nbsp;Anyway this is all I know about what your trying to solve.<br><br>Save Offen,<br><br>Joe :)
 
Joe2k,<br><br>No, I don't mean I can't use JavaScript.&nbsp;&nbsp;In fact, that's what I am trying to do!&nbsp;&nbsp;nWhat I meant is that most of the JavaScript code needs to be in a separate file outside the HTML as it is now, rather than being embedded in it.&nbsp;&nbsp;I appreciate everyone's help and thank you for it, but I am certain there must be a simpler way that we have not yet discovered.&nbsp;&nbsp;On the surface (coming from a non-programmer as myself), it seems like it would be a simple thing to do.<br><br>Don
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top