Radio button validator

How can I check the radio buttons? Because it does not work. Radio button nameand IDis equal billable, and they value- or yes, or no.

function formValidator() {  
  var errors = new Array();   

  if($("#billable").checked == false) {  
    errors[0] = "*Billable - Required";  
  }

  if(errors.length > 0) {  
    var error_msg = 'Please review:';  

    for(i=0;i<errors.length;i++) {  
      if(errors[i]!=undefined) {  
        error_msg = error_msg + "\n" + errors[i];  
      }  
    }  

    alert(error_msg);  

    return false;  
  }

  return true;  
}
+3
source share
2 answers

Change to

$("#billable:checked").size()

Using $("#billable").checked, you will receive undefined, because this property does not exist. With the help of $("#billable:checked").size()you will get the number of checked radio stations (0 or 1)

See jsfiddle .

0
source

if you ever try with the jQuery validation plugin , you will need the following:

rules: {
'billable[]':{ required:true }
}

Edited by:

, :

var iz_checked = true;
$('input').each(function(){
   iz_checked = iz_checked && $(this).is(':checked');
});
if ( ! iz_checked )
0

Source: https://habr.com/ru/post/1761216/


All Articles