JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

HTML5 Required Attribute

March 18, 2014 by Krishna Srinivasan Leave a Comment

The required attribute is a form attribute introduced in HTML5. It is used to notify that we have to fill the particular field with some data before submitting the form. This attribute can be used for validations, which otherwise was done using JavaScript code. This attribute saves a lot of development time (validation code development) . The required attribute is of type Boolean. This tutorial explains the basic usage of required attribute with simple example.

Syntax of required Attribute

[code lang=”html”]
<input type= "string" required>
[/code]

HTML5 Required Example

[code lang=”html”]
<!DOCTYPE html>
<head>
<title> Required Attribute</title>
</head>
<body>
<form>
Name <input type="text" required><br>

Surname<input type="text" required><br>

DOB <input type="text" required><br>

Residential Address<input type="text" required><br>

Phone Number <input type="text" required><br><br>

<input type="submit" value="Submit">

</form>
</body>
</html>
[/code]

In this example we are validating all the fields using the required attribute. Now suppose you don’t enter the fields with values and click on submit button, a validation error occurs and a message is displayed.

Example Application Test

  • Save the file as required_example.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser. Note that the browser must support HTML5 specification.

Output

When the execution process is completed successfully we will get the following output :
HTML5 Required Attribute
Now click on the submit button without entering any values in the fields, a validation error occurs and an output as below would be seen:
HTML5 Required Attribute1

Filed Under: HTML Tagged With: HTML5

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved