One method is to use data validation, which is the process of verifying that the data your site visitors enter into the form fields is correct. There are two ways to tackle that process:
- On the server, with server-side programming code
- In the client browser, using HTML, CSS, and JavaScript
This is where client-side data validation comes in handy. The more data you can validate in the browser as the site visitor enters it, the better the chance you have of receiving valid data in the first place.
Holding your place
HTML5 helps that process with a few additional features. One such feature is theplaceholder
attribute for the input
element. The placeholder
attribute appears as gray text inside the form field and can provide a suggested format for the data to enter:
<label> Enter your daytime phone number: <input type="tel" name="num" placeholder="(nnn)nnn-nnnn"> </label>The browser displays the placeholder value inside the input form field, but as gray text.

As you start typing text in the input field, the placeholder text disappears.
Making certain data required
Another data validation attribute added by HTML5 is therequired
attribute:
<input type="text" name="lastname" required="required">The
required
attribute marks the form field so that the browser won't upload the form if that field is empty. Some browsers will display an error message indicating which required form field(s) are empty.
Validating data types
Not only do the additional HTML5 input types produce different types of input fields, but you can also use them to validate data. Browsers that support the new HTML5 data types will mark input form fields that contain data not in the proper format with the invalid state.CSS provides pseudo-class rules to style elements based on their state. You use the invalid
and valid
pseudo-class states to style input fields with invalid data differently from input fields with valid data. This helps make the fields with invalid data stand out in the form.
Here's a quick example you can try to test this feature:
- Open your favorite text editor, program editor, or IDE package.
- Type the following code:
Testing for Invalid Data Testing for invalid data
- Save the file as
invaliddatatest.html
in theDocumentRoot
folder for your web server (c:\xampp\htdocs
for XAMPP on Windows or/Applications/XAMPP/htdocs
for XAMPP on macOS). - Start the Apache web server from XAMPP.
- Test your work here.
- Close the browser, and stop the XAMPP web server.