Enrique Moreno Tent Enrique Moreno Tent - 1 year ago 107
CSS Question

Validation in HTML5. :invalid classe after submit

I'm building a form and I want to use the

selector to give the "required" input fields a red border if the user presses submit without filling them, but using this makes them appear highlighted right when the page loads. It seems unfriendly to give this kind of warning to the user before even giving him the chance to fill them at least once.

Is there a way that these fields appear highlighted only after trying to submit the form, said in another way, is there a way to run the validation only after clicking submit (or at least losing focus on the required input fields?)

Answer Source

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:

    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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download