Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 form required attribute. Set custom validation message?

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

like image 271
Skizit Avatar asked Mar 11 '11 11:03

Skizit


People also ask

Which HTML5 attribute is used for data validation?

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.

How do I create a custom validation in HTML?

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.


1 Answers

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('')" 
like image 190
Somnath Kadam Avatar answered Sep 20 '22 22:09

Somnath Kadam