We help IT Professionals succeed at work.

Form validation question

earwig75
earwig75 asked
on
44 Views
Last Modified: 2015-08-18
I have a Div with a class called "ss-item-required". In that div  I have some text boxes and radio buttons. I want to validate each set of radio buttons and input boxes. The radio button validation is working, but the text boxes are not validating. Below is the code I am using to check that the radio buttons are validated. Can someone help me add to this script so that any input boxes in the div with that name are also validated? Thank you.
 
        if ($('div.ss-item-required:not(:has(:radio:checked))').length > 0) {
    	alert("Make a selection");
    	return false;

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
By validated do you mean has content?
Sample code below - full working source here
<script type="text/javascript">
$(function() {
  $('form').submit(function() {
   // Init error message 
    var msg = '';
    // Find all input's with the ss-item-required class
    $('input.ss-item-required').each(function() {
      // If empty add to error message using
      // placeholder text as label
      if ($(this).val() == '') {
        msg += $(this).attr('placeholder') + " is required\n";
      }
    });
    // Msg not empty - errors so show and return
    // false to prevent form submission
    if (msg !== '') {
      alert("Error: " + msg);
      return false;
    }
    // No errors - return  true so form can
    // be submitted
    return true;
  });
});
</script>

Open in new window

Author

Commented:
This doesn't seem to work with a div class. Is it possible to just add to my existing validation? Basically a repeat of the line below checking for a checked radio button, but check that an input box in that DIV has content? I am not using labels or classes on the inputs.

Thanks again.

        if ($('div.ss-item-required:not(:has(:radio:checked))').length > 0) {
    	alert("Make a selection");
    	return false;

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.