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
Use
setCustomValidity
:I changed to vanilla JavaScript from Mootools as suggested by @itpastorn in the comments, but you should be able to work out the Mootools equivalent if necessary.
Edit
I've updated the code here as
setCustomValidity
works slightly differently to what I understood when I originally answered. IfsetCustomValidity
is set to anything other than the empty string it will cause the field to be considered invalid; therefore you must clear it before testing validity, you can't just set it and forget.Further edit
As pointed out in @thomasvdb's comment below, you need to clear the custom validity in some event outside of
invalid
otherwise there may be an extra pass through theoninvalid
handler to clear it.