help with javascript function

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) ||
		{	
			checkboxes=1;
		}
		else if (checkboxes=0)
			{
				alert('You must check at least one box on the form');
				return false;
			}	   
		else
        {
            doFormSubmit(document.MyForm);
        }
    }
</script>

Open in new window


The Submit Button:

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

Open in new window

AxISQSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
Given the following HTML

<form>
  <input type="text" required>
  <div>
    <label>Check One</label>
    <ul>
      <li>
        <input type="checkbox"> checkbox 1</li>
      <li>
        <input type="checkbox"> checkbox 2</li>
      <li>
        <input type="checkbox"> checkbox 3</li>
      <li>
        <input type="checkbox"> checkbox 4</li>
    </ul>
  </div>
  <button id="btnSubmit" type="button">
    Submit
  </button>
</form>

Open in new window


You could do something like:
function validateForm(obj) {
  var requiredElements = document.querySelectorAll("[required]");

  var valid = true;
  for (var i = 0; i < requiredElements.length; i++) {
    if (requiredElements[i].value === "") {
      valid = false;
    }
  }

  var checkboxElements = document.querySelectorAll('input[type=checkbox]');
  for (var i = 0; i < checkboxElements.length; i++) {
    if (checkboxElements[i].checked) {
      logResult(valid);
      return;
    }
  }
  logResult(false);
}

var btnSubmit = document.getElementById("btnSubmit");
btnSubmit.addEventListener('click', validateForm);

function logResult(result) {
console.log(result);
}

Open in new window


If you're not opposed to jQuery, you could use a validation plugin: https://jqueryvalidation.org/ 
This option offers more robust and flexible validation.  However, if you simply need to verify that required inputs have something in them, and that at least one checkbox is selected, you could use a simple validation like the one I have given you an example for.

Here's a Fiddle Demo:  https://jsfiddle.net/zephyr_hex/f00n0yv8/
0
Julian HansenCommented:
I have added code tags to your post. For more information on code tags please read this link http://support.experts-exchange.com/customer/portal/articles/349945

To find out if at least one checkbox is checked you can do this

var els = document.querySelectorAll(':checked');
if (els.length == 0) alert('Please select at least one checkbox');

Open in new window

For the inputs - you can use the attribute required but if you are going to handle this yourself best not to as you will be bumping into the browsers validation. If you do your own give the required inputs a class (ex. required) and then use that to do the validation

Here is a complete validate function that checks for at least on checked checkbox and required fields completed
function validate()
{
 // Check 1 check box checked
  var els = document.querySelectorAll(':checked');
  if (els.length == 0) alert('Please select at least one checkbox');
  
  // validate the required inputs
  var inpts = document.querySelectorAll('.required');

  for(var i = 0; i < inpts.length; i++) {
    if (inpts[i].value == '') {
      alert('A value is required for ' + inpts[i].name);
    }
  }
  
  return false;
}

Open in new window


Working sample here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxISQSAuthor Commented:
thanks guys.  the code examples helped
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.