简体   繁体   中英

Check if a form field is not empty

I am trying to check if an input field is not empty. If not empty, process the form. The field is ID of ninja_forms_field_66. The problem is that when I enter "0" in this field, it thinks the field is empty and it won't pass validation.

$(document).ready(function() {  
    $('#ninja_forms_field_50').click(function() { 
        var ninja_forms_field_46=$( "#ninja_forms_field_46" ).val();
        if( (ninja_forms_field_46 !='' && ninja_forms_field_301 !='' && ninja_forms_field_66 !='undefined' && ninja_forms_field_71 !='' && ninja_forms_field_75 !='' && ninja_forms_field_304 !='' && ninja_forms_field_64 !='' && ninja_forms_field_63 !='' && ninja_forms_field_95.checked) || (document.getElementById("47_checklist").checked == true) )    

        {
        $.blockUI({ message: $('#rLo') });  
        }
    }); 
}); 

edit: I have tried it like ninja_forms_field_66 !='' and ninja_forms_field_66 !='undefined'

Check the length of the field: ninja_forms_field_46.length > 0

You should also trim the value:

 var ninja_forms_field_46= $.trim($("#ninja_forms_field_46" ).val());

尝试

if(ninja_forms_field_66) {...}

我会建议你使用字符串长度的功能,并检查它是否大于0 - str.length> 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM