Monday 15 August 2011

Check if multiple textfields are empty jQuery -



Check if multiple textfields are empty jQuery -

this might simple i'm having difficulties here. i'm running required attribute through fields of .um_frm , checking if fields not empty. problem instead of checking fields, passes through if single field filled. how create sure fields filled?

$('#um_ok').on( 'click', function() { $('.um_frm').attr('required','required'); var value=$.trim($(".um_frm").val()); if(value.length === 0){ //proceed... } });

i tried not suiccessful

$('#um_ok').on( 'click', function() { $('.um_frm').attr('required','required'); if($.trim($(".um_frm").val()) === ""){ //proceed... } });

use filter see if field blank (length not truthy, i.e. 0, hence !):

$('#um_ok').on( 'click', function() { $('.um_frm').attr('required','required'); var anyblank = $(".um_frm").filter(function(){ homecoming !$.trim($(this).val()).length; }).length; // if not blanks... if(!anyblank){ //proceed... } });

update: (thanks @george)

as required genuine html element property , not attribute, should utilize prop (which can take more readable boolean value on/off state):

$('.um_frm').prop('required', true);

this has advantage of creating cleaner required attribute no value (instead of required="required" etc).

jquery

No comments:

Post a Comment