HTML5 Form Validation

Best WordPress Hosting

Form validation is an important thing we nee to do while creating a web based form. In early days, developers used server side form validation. Then it came client side form validation. Now with HTML5, we can easily validate a form. All modern browsers supports HTML5 form validation.

Although HTML5 has field validation feature, there are many developer who still do not know about this and use jQuery validation. In this, post I will describe how to use HTML5 form validation in your web application.

Read: Form Validation With JQuery and CSS

HTML5 Form Validation

For, client side validation, HTML5 introduces various kind of new INPUT elements with validation attributes  Below, I will explain all input types and attributes. For validation, you need to use proper input type with attribute and pattern. Here pattern is used to define a pattern of accepted values. It is optional but you need to know how to use it properly.

Input Types

Now, we have data specific input types. These input types are email, url, number, tel, date and many others. If you want to ask user’s email address, use this code to add HTML5 Form Validation for email field

Similarly you can use other HTML5 input types.

These are the input types and their use

  • color – It allows users to color
  • date – It allows users to select date
  • datetime – It allows users to select date and time with timezone
  • datetime-local – similar to last one by without timezone
  • email – similar to text field but user should enter email
  • month – It allows users to select a month
  • number – It should contain a numeric value/
  • range – It should contain value from a range
  • search – It behaves like a regular text field but used for search box.
  • tel – It will be used for telephone numbers
  • time – For taking time
  • url – It is for URL
  • week – The week type allows the user to select a week and year.

Required Attribute

This is the attribute which must be add in the INPUT field if you want its value to be validated. If you add required, user must input some value in the field in order to submit form.

If you add required, it informs web browser that the field is mandatory.

<tr><td>Email</td><td><input type=”email” placeholder=”Enter Valid Email” required /></td></tr>

You can use this to al fields which are mandatory.

Input form

INPUT patterns

This pattern is used while validation. see the pattern for URL input type.

input type=”url” pattern=”https?://.+”

Now see the password field validation below.

If you want to take a look on a form with most of the validation, see the code below.

It will be shown like the snap below:

HTML5 form validation

advertisement

About The Author

  • ADD YOUR COMMENT

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Receive fresh web design resources and SEO tips in your email by our weekly newsletter

You have Successfully Subscribed!