Validation in HTML5. :invalid classe after submit

前端 未结 9 2481
清歌不尽
清歌不尽 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:42

    No there is nothing out of the box.

    Mozilla has its own pseudoclass for something very similiar called ':-moz-ui-invalid'. If you want to achieve something like this, you have to use the constraint validation DOM-API:

    if(document.addEventListener){
        document.addEventListener('invalid', function(e){
            e.target.className += ' invalid';
        }, true);
    }
    

    You can also use webshims lib polyfill, which will not only polyfill incapable browsers, but also adds something similiar like -moz-ui-invalid to all browser (.form-ui-invalid).

提交回复
热议问题