View Full Version : How to make sure a user selected something in a form?

11-27-2012, 12:04 AM
Hey guys I am working on a project and our teacher is making us make sure that at least one of the group of text boxes, check boxes, and selection lists are checked. How would I do this? Any help would be greatly appreciated!


<strong>Select your current class rank:</strong><br /> <!--Displays the class rank radio buttons//-->
<input type="radio" name="registrationrank" value="Freshman" />Freshman
<input type="radio" name="registrationrank" value="Sophomore" />Sophomore
<input type="radio" name="registrationrank" value="Junior" />Junior
<input type="radio" name="registrationrank" value="Senior" />Senior

<p>Select your major(s).</p> <!--Displays the major check boxes//-->
<input type="checkbox" name="majors" value="business" />Business<br />
<input type="checkbox" name="majors" value="cet" />Computer Engineering Technology<br />
<input type="checkbox" name="majors" value="cis" />Computer Information Systems<br />
<input type="checkbox" name="majors" value="cs" />Computer Science<br />
<input type="checkbox" name="majors" value="other" />Other<br />

<h3>Select the semester for which you are registering.</h3>
<p><select name="semester">
<option value="">Choose One:</option>
<option value="fall">Fall</option>
<option value="spring">Spring</option>
<option value="summer">Summer</option>
<option value="special_session">Special Session</option>

<h3>Select the courses for which you are registering.</h3> <!--Displays the courses drop down list and all can be selected//-->
<p><select multiple="multiple">
<option value="cis110">CIS 110 Intro to Information Systems</option>
<option value="cis120">CIS 120 Application Programming I</option>
<option value="cis220">CIS 220 Application Programming II</option>
<option value="cis299">CIS 299 Systems Analysis I</option>
<option value="cis322">CIS 322 Database Application Development</option>
<option value="cis332">CIS 332 Web Programming II</option>
<option value="cis341">CIS 341 CISCO CCNA 1</option>
<option value="cis343">CIS 343 CISCO CCNA 3</option>
<option value="cis352">CIS 352 Global, Economic and Social Ethical Issues in Computing</option>
<option value="cis492">CIS 492 Senior Project II</option>


Those are all of my items that are included. Thank you for your time!

Old Pedant
11-27-2012, 12:06 AM
Look here:


Look at Rule 1.5.


You make an effort and if you run into a bug we will likely help you. But don't expect us to do your homework for you.

One hint: Google (or even search this forum) for "form validation".

11-27-2012, 01:21 AM
sorry this isn't my whole hw but a tiny part of a large assignment but I apologize I wasn't expecting anyone to do it for me just suggestions. But I might have found something but I cant get it to work right. I have made a function that is supposed to check to see if the class ranking is selected.


function classRankCheck() {
for (var i=0;i<document.forms[0].registrationrank.length;i++)
var rankselect=true;

window.alert("Please select a class rank!");
return false;


Am I close to this at all? haha....

Old Pedant
11-27-2012, 02:27 AM
Two bugs:

(1) You didn't initialize the variable rankselect so if nothing is selected it won't have any value at all. So initialize the variable to false *before* the loop and don't use var inside the loop.

(2) Ummm...where did productselect come from?

(3) [Extra credit, though your instructor may not like it] How can you code that without needing the rankselect variable, at all?


I have to ask: Why is "majors" a set of checkboxes? Are you expecting people to check more than one major???

11-27-2012, 02:42 AM
actually yes! lol our teacher made sure to tell us that they could select more than one major at a time.....(idk why)

and product select was a mistake haha that was supposed to be rank select

thanks for all of your assistance though...I am not very good at any coding yet =[ but I am getting there!