I've got the following HTML5 form: http://jsfiddle.net/nfgfP/
<form id="form" onsubmit="return(login())">
<input name="username" placeholder="Username" required />
<input name="pass" type="password" placeholder="Password" required/>
<br/>Remember me: <input type="checkbox" name="remember" value="true" /><br/>
<input type="submit" name="submit" value="Log In"/>
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
Here is the code to handle Custom Error Message in HTML5
This part is important because it hides the error message when the user inputs new data:
The easiest and cleanest way I've found is to use a data attribute to store your custom error. Test the node for validity and handle the error by using some custom html.
le javascript
and some super HTML5
I have a simpler vanilla js only solution:
For checkboxes:
For inputs:
I have made a small library to ease changing and translating the error messages. You can even change the texts by error type which is currently not available using
title
in Chrome orx-moz-errormessage
in Firefox. Go check it out on GitHub, and give feedback.It's used like:
There's a demo available at jsFiddle.
Try this one, its better and tested:
HTML:
JAVASCRIPT:
Demo:
http://jsfiddle.net/patelriki13/Sqq8e/
By setting and unsetting the
setCustomValidity
in the right time, the validation message will work flawlessly.I used
onchange
instead ofoninput
which is more general and occurs when the value is changed in any condition even through JavaScript.