HTML5 form required attribute. Set custom validation message?

Skizit picture Skizit · Mar 11, 2011 · Viewed 572.4k times · Source

I've got the following HTML5 form: http://jsfiddle.net/nfgfP/

Currently when I hit enter when they're both blank, a popup box appears saying "Please fill out this field". How would I change that default message to "This field cannot be left blank"?

EDIT: Also note that the type password field's error message is simply *****. To recreate this give the username a value and hit submit.

EDIT: I'm using Chrome 10 for testing. Please do the same

Answer

Somnath Kadam picture Somnath Kadam · Nov 25, 2013

Here is the code to handle custom error message in HTML5:

<input type="text" id="username" required placeholder="Enter Name"
  oninvalid="this.setCustomValidity('Enter User Name Here')"
  oninput="this.setCustomValidity('')"/>

This part is important because it hides the error message when the user inputs new data:

oninput="this.setCustomValidity('')"