Form validation in jQuery without using plugin

前端 未结 4 1561
悲&欢浪女
悲&欢浪女 2020-12-19 07:50

I want to perform client-side validation of a simple form using jQuery, but I can\'t use any plugins for validation. I want to display any error messages in a single alert

4条回答
  •  眼角桃花
    2020-12-19 08:40

    If you only want 1 alert to appear at a time you need to check the state of valid for each condition:

    $('#submit').on('click', function() {
        valid = true;   
    
        if (valid && $('#name').val() == '') {
            alert ("please enter your name");
            valid = false;
        }
    
        if (valid && $('#address').val() == '') {
            alert ("please enter your address");
             valid = false;
        }    
    
        return valid;
    });
    

    Updated fiddle

提交回复
热议问题