Automatic php form validation with HTML5 (required)

Written by - 0 comments

Published on - Listed in Internet HTML PHP

I just realized that I love HTML5. I'm programming a PHP form and I remember that 10 years ago I had to verify and sanitize all form input types if they were set/filled in by checking the $_POST data.

Now with HTML5 the job got a lot easier (and faster!). Instead of manually analyzing the submitted data after the user hit post/submit, the browser can now do the job on his own. The magic is done by the "required" parameter in a form element.

For instance:

<input type="radio" name="iEnvironment" value="prod" required="true" />PROD
<input type="radio" name="iEnvironment" value="qual" required="true" />QUAL
<input type="radio" name="iEnvironment" value="stage" required="true" />STAGE
<input type="radio" name="iEnvironment" value="test" required="true" />TEST

When the user submits the form, the browser verifies if the required fields are selected. In this case the radio button with name "iEnvironment" needs to be set. So at least one of the four options must be selected. If the user doesn't, here's what the browser says:

HTML5 form validation with required parameter

It's been a long time since I last programmed a web form in PHP and this new feature just hit me now. Thanks HTML5!

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.