Using jQuery to prevent form submission when input fields are empty

后端 未结 5 827
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 08:38

The solution should be pretty straightforward. I\'m trying to prevent the form from submitting properly when no value is found within the input boxes. Here\'s my JSFiddle: h

5条回答
  •  爱一瞬间的悲伤
    2020-12-14 09:17

    I guess that this will help:

    $('#form').submit(function(e) {
        e.preventDefault();
        $("#email, #user_name").each(function(){
            if($.trim(this.value) == ""){
                alert('you did not fill out one of the fields');
            } else {
                // Submit 
            }
        })
    })
    

提交回复
热议问题