We help IT Professionals succeed at work.
Get Started

help with javascript function

AxISQS asked
Last Modified: 2017-11-21
I have an HTML form with some checkboxes and a couple of required fields on it.  I need the submit button to check that at least 1 checkbox is checked and the 2 required fields have values.  Right now, clicking submit doesn't do anything on the form.  Does the logic of the function and submit button look correct?

The Function:

<script type="text/javascript" language="javascript">
function validateFormFields() 
		var checkboxes=0;
        if (document.getElementsByName('_1_1_2_1') == "")
            alert('Please make a selection before submitting the form');
            return false;
        else if (document.getElementsByName('_1_1_3_1').value == "")
            alert('You must make a selection before form submission');
            return false;
		else if 		
	   	(document.getElementsByName('_1_1_62_1').checked) ||(document.getElementsByName('_1_1_61_1').checked) ||
                (document.getElementsByName('_1_1_60_1').checked) ||(document.getElementsByName('_1_1_59_1').checked) ||
                (document.getElementsByName('_1_1_57_1').checked) ||(document.getElementsByName('_1_1_56_1').checked) ||
                (document.getElementsByName('_1_1_54_1').checked) ||(document.getElementsByName('_1_1_53_1').checked) ||
                (document.getElementsByName('_1_1_52_1').checked) ||(document.getElementsByName('_1_1_51_1').checked) ||
                (document.getElementsByName('_1_1_50_1').checked) ||(document.getElementsByName('_1_1_49_1').checked) ||
                (document.getElementsByName('_1_1_48_1').checked) ||(document.getElementsByName('_1_1_47_1').checked) ||
		else if (checkboxes=0)
				alert('You must check at least one box on the form');
				return false;

Open in new window

The Submit Button:

<INPUT CLASS="applyButton" TYPE="BUTTON" VALUE="Submit" NAME="IgnoreMe" ONCLICK="validateFormFields();">
<INPUT CLASS="resetButton" TYPE="RESET" VALUE="Reset"> 

Open in new window

Watch Question
Most Valuable Expert 2017
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE