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
The pattern attribute of the <input> element allows you to add basic data validation without resorting to JavaScript. It works by matching the input value against a regular expression.
Use setCustomValidity() to set the message of the input affected to override the default message when the form is submitted. See the updated fiddle. As you can see in the fiddle, all you have to do is add an attribute data-equal-id wherein the attribute value must be the ID of password input element to be tested.
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('')"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With