Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating radiobuttons in jquery

I'm using the following to make sure fields aren't empty before they get posted in jquery:

if(this.field.value == "") {
  jQuery( ".page-error-message" ).remove();
  jQuery(".top").append(jQuery("field required"));
  jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
   return;
}

I have some radio buttons that need yes selected, however it doesn't seem to work if I do say:

if(this.radiobutton.value == "no") {
  jQuery( ".page-error-message" ).remove();
  jQuery(".top").append(jQuery("field must be yes"));
  jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
   return;
}

What am I doing wrong?

like image 308
David Avatar asked May 23 '26 05:05

David


1 Answers

Radio buttons have multiple fields. You're better checking that the no button is checked.

eg;

if(this.radiobutton.checked) {
  jQuery( ".page-error-message" ).remove();
  jQuery(".top").append(jQuery("field must be yes"));
  jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
  return;
}

Where radiobutton is the radio button with a value of no.

like image 83
Kevin Choppin Avatar answered May 25 '26 20:05

Kevin Choppin