Validation in HTML5. :invalid classe after submit

前端 未结 9 2489
清歌不尽
清歌不尽 2021-02-18 14:11

I\'m building a form and I want to use the :invalid selector to give the \"required\" input fields a red border if the user presses submit without filling them, but

9条回答
  •  无人及你
    2021-02-18 14:40

    for 'required' validation

    way 1 - set 'required' attribute for each element on form submit

    // submit button event
    $('#form-submit-btn').click(function(event) {
        // set required attribute for each element
        $('#elm1, #elm2').attr('required','true');
    
        // since required attribute were not set (before this event), prevent form submission
        if(!$('#form')[0].checkValidity())
            return;
    
        // submit form if form is valid
        $('#form').submit();
    
    });
    

    way 2 - use 'data' attribute

    
    
    
    

提交回复
热议问题