Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you style the HTML5 form validation messages?

Tags:

html

forms

Say you have some HTML like this:

<form>     <input placeholder="Some text!" required>     <input type="email" placeholder="An Email!" required>     <input type="submit" value="A Button!"> </form> 

Because of the required attributes, newer Webkits and Firefoxes show a validation message next to the field if you leave it blank.

They respond to being styled by a rule such as:

div {     font: Helvetica; } 

But I can't find a more specific selector for them. Does anyone know what selector is used, or will be used, or even a bug report for webkit/gecko relating to this?

( JSFiddle showing that they can be styled with a div selector: http://jsfiddle.net/p7kK5/ )

like image 243
Rich Bradshaw Avatar asked Apr 19 '11 08:04

Rich Bradshaw


People also ask

What is HTML5 form validation?

With HTML5, form validation is a built-in feature. There are various validation attributes that come with HTML5. When form input is valid, the :valid CSS pseudoclass is applied to the element. If it's invalid, then the :invalid CSS pseudoclass is applied to the element.

How do I create a custom validation in HTML?

The setCustomValidity method allows you to set a custom text by changeing the validationMessage property of the DOM node that contains the message. When the input is checked by checkValidity method on a form element node, and found to be invalid, the invalid event is fired for the node.

Which is the correct syntax for email validation in HTML5?

The <input type="email"> defines a field for an e-mail address. The input value is automatically validated to ensure it is a properly formatted e-mail address. To define an e-mail field that allows multiple e-mail addresses, add the "multiple" attribute.


1 Answers

Update: Chrome does not allow styling form validation bubbles anymore: https://code.google.com/p/chromium/issues/detail?id=259050

In the latest iterations of Chrome, support has been added for pseudo selectors for these, namely;

::-webkit-validation-bubble{} ::-webkit-validation-bubble-top-outer-arrow{} ::-webkit-validation-bubble-top-inner-arrow{} ::-webkit-validation-bubble-message{} 

For more information on these, check out the Webkit "Styling Form Controls" trac page.

Additionally, firefox has support for the element attribute x-moz-errormessage which enables you to change the text of the error message, which is something you could do in Chrome using CSS to and -webkit-validation-bubble-message. See more about x-moz-errormessage on the MDN Docs page.

As of yet, Firefox has no way to style the error bubbles.

like image 145
Keithamus Avatar answered Sep 21 '22 17:09

Keithamus