简体   繁体   中英

jQuery form fields validation

i have a form with few input fields with class "inputfields" need to validate each field if empty alert 'fieldname is empty' otherwise return true my jquery code is not working keep getting errors at the console log can any one help please ?

jQuery(document).ready(function() {

        $('.inputfields').each.on('change keyup' ,function(){
            var ope = $(this).attr('name');
            if (ope.val()==''){
                alert(ope+'is empty');
                }else {
                    console.log(ope);
                    }
                });
    });

I believe you want to check different types of input fields, hence want to use change and keyup .

I tried something based on your code, but this following solution will only work, if you want to validate text type input fields. For select or other input types you have to put some more checks inside the loop or have to some other way to validate.

    $('.inputfields').each(function() {
        $(this).bind('change keyup', function() {
            var ope = $(this).attr('name');
            if ($(this).val() == '') {
                console.log(ope+'is empty');
            } else {
                console.log(ope+ ' : ' + $(this).val());
            }
        });
    });

Hope this will lead you to find your desired solution.

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