html5-tutorialsmade

Simple HTML5 form validation demo

Almost all of us know how to do a JavaScript validation, but you know there is a simple way to validate a form using HTML5?

Yes, it’s true but many of us does not know validation can be done in HTML5 itself, this tutorial I’m going to show you is a simple example of how to do make all the fields mandatory.

Let’s see the code:

You got something after reading the code?

If not, I will tell you what, there are new set of input types and attributes introduced in HTML5, using that we can do all kind of form validations, in the above example if you see I have added few new input types such as,

input=”email”
input=”url”
input=”number” 

and an attribute called “required” in all the fields.

So just adding a required attribute makes all the fields mandatory and the new input types validates if you not entered a valid email, url or a number in the respective fields.

You can also validate a field using regular expression using a new attribute called “pattern

For instance, If you want to validate the name field, lets say you want at least three characters should be typed in the name field, I can add a reg ex patter there like below:

See the demo in here:

demo

 

Related Posts

Leave a Reply