Gaining an Understanding of Using Bootstrap Forms and Validations

Posted by David Watson . on November 24, 2014

In this tutorial, we are going to learn about the methods and ways through which Bootstrap 3 forms can be used for creating responsive contact forms. We will also discuss about validation classes that help you validate user input if you want to create a form and its validation simultaneously.

HTML forms are a crucial part of a website. They help you get in touch with your customers and also assist in performing a number of tasks including newsletter subscription, giving feedbacks, making status in social media and so on. But styling the forms manually one by one through CSS is a time consuming process. However, Twitter Bootstrap simplifies the process of styling these contact forms and their areas including input boxes, select boxes, text areas etc.

 

Twitter Bootstrap mainly provides three types of forms layouts.

1. Vertical forms ( which is basically a default form layout)
2. Horizontal forms
3. Inline form

In this post, we will learn about three of them and their making in detail.

Understanding The Basics of Twitter Bootstrap 3

Before we actually take you to the topic, let’s first set up Bootstrap files and an HTML page. So, let’s get started with copying the following HTML form.

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Forms in Bootstrap – Demo</title>
<!– Bootstrap –>
<link rel=”stylesheet” href=”//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css”>
<!– HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries –>
<!– WARNING: Respond.js doesn’t work if you view the page via file:// –>
<!–[if lt IE 9]>

<script src=”https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js”></script>
<script src=”https://oss.maxcdn.com/respond/1.4.2/respond.min.js”></script>
<![endif]–>
</head>
<body>
<div>

<h1>Hello World</h1>
</div>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”></script>
<script src=”//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js”></script>
</body>
</html>

The above code is basically a Bootstrap based markup, which is necessary to make it available seamless across a variety of devices. The framework is provided with all the needed meta tags, Bootstrap’s CSS and JavaScript from the CDN, along with the jQuery file. Since, the Internet Explorer 8 framework currently does not support CSS3 and HTML5, it is advisable to integrate html5shiv and respond.js files, so that both of them can be made compatible with each other.

In the above markup there is also a Bootstrap container which includes several components for it. It’s div element which consists class.container to create fixed layouts. It is also helpful in wrapping and positioning of the elements to the middle of the browser window.

So, up to this point, you have gained a pretty good understanding of Bootstrap framework and the components related to it. In the next part, we will learn about how different forms can be created using the same.

But before we start creating forms, let’s get a basic HTML

tag and add it inside the markup container which we have mentioned above. It will look like this:

1. Creating Vertical Form Layout

Vertical forms are default Bootstrap forms in which styling can be applied to form controls without adding base class to the element or providing any extra changes to the markup.

In the below mentioned example, you can see that the layout is stacked with left-aligned labels placed at the top.

<form>
</form>
<form>
<div class=”form-group”>
<label for=”add email address”>Email</label>
<input type=”email” class=”form-control” id=”add email address” placeholder=”Email”>
</div>
<div class=”form-group”>
<label for=”providePassword”>Password</label>
<input type=”password” class=”form-control” id=”providePassword” placeholder=”Password”>
</div>
<div class=”checkbox”>
<label><input type=”checkbox”> Remember me</label>
</div>
<button type=”submit” class=”btn btn-primary”>Login</button>
</form>

The Output will look something like this:

2. Creating Horizontal Form Layout

In Horizontal form layout, labels are generally aligned on the right and floated on the left so that they can be appeared in the same line just as form controls. While creating horizontal forms, you need to try your hands with various changes within the markup. The following are the steps you need to follow:

  • in the <form> element add the class .form-horizontal
  • Inside the <div> element, start wrapping and labeling form controls and apply the class .form-group
  • For aligning these labels and form controls, make use of Bootstrap’s <grid classes>
  • Now, lastly add the class .control-label in the <label> element.

 You can copy this code as well:

<form class=”form-horizontal”>
<div class=”form-group”>
<label for=”enterEmail” class=”control-label col-xs-2″>Email</label>
<div class=”col-xs-10″>
<input type=”email” class=”form-control” id=”enterEmail” placeholder=”Email”>
</div>
</div>
<div class=”form-group”>
<label for=”providePassword” class=”control-label col-xs-2″>Password</label>
<div class=”col-xs-10″>
<input type=”password” class=”form-control” id=”providePassword” placeholder=”Password”>
</div>
</div>
<div class=”form-group”>
<div class=”col-xs-offset-2 col-xs-10″>
<div class=”checkbox”>
<label><input type=”checkbox”> Remember me</label>
</div>
</div>
</div>
<div class=”form-group”>
<div class=”col-xs-offset-2 col-xs-10″>
<button type=”submit” class=”btn btn-primary”>Login</button>
</div>
</div>
</form>

3. The Process of Creating Inline Forms

There are situations when you need to create a form wherein all the elements are needed to be placed horizontally to compact the layout. You can do this easily by adding a Bootstrap class .form inline to the element in the above markup.

The output will look like this:See, it wasn’t that hard right?Now, in the next part, let’s gain an understanding about Validation class.

What is Validation Classes?

If you are not interested to use HTML5 and believe in writing your own JavaScript codes, then chances are pretty good that you must need to take the help of Validation Classes provided by Bootstrap. These classes are extremely helpful in designing forms that ensure a high level of user experience on a website.

There are mainly three types of input provided by Validation classes:

1. Input with Success- .has-success is applied to give a green border to the text field
2. Input with Warning- .has-warning is applied to give brown border to the text field
3. Input with Error- .has-error is applied to give red border to the text field.

All the above mentioned Validation classes can be added along with the .form-control classes.

<div class=”form-group has-success has-inquiry”>
<input type=”text” class=”form-control”>
<span class=”glyphicon glyphicon-ok form-control-inquiry”></span>
</div>

If you love to play with striking icons then add the below mentioned codes to each text field while validating.

Not to forget adding .has-inquiry to the .form-group element and class .form-inquiry to the span element. In the above example, we have used the icon Glyphicom, which comes by default with the Bootstrap framework.

Conclusion

Hopefully, you found the tutorial helpful in gaining a brief understanding about the Twitter Bootstrap framework, various forms and Validation Classes. You can now style the forms in your own way and make them attractive for your customers.

Author Bio – Sarah Parker is a professional WordPress developer associated with Designs2HTML Ltd – a leading PSD to WP conversion company providing complete web development services to the clients across the world. She is also a passionate writer who loves to write articles on latest trends in WP website design and development.

Leave a Comment

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