St. Louis Website Design

How To Implement Parsley With The Bootstrap Framework

In most websites, we need to collect user details and or other types of information by using forms and we are required to have form validations to make sure user input is correct and useful. This can be done easily by using Parsley with the Bootstrap Framework.

Typical Validation Tasks Are:

  • Has the user filled in all required fields?
  • Has the user entered a valid date?
  • Has the user entered text in a numeric field?

Most Often, The Purpose Of Form Validation Is To Ensure Correct User Input

Parsley

Parsley is a javascript form validation library. It helps you provide your users with feedback on their form submission before sending it to your server. It saves you bandwidth, server load and it saves time for your users.

Javascript form validation is not necessary, and if used, it does not replace strong backend server validation.

Bootstrap

Bootstrap is an open source toolkit for developing with HTML, CSS, and JS. Quickly prototype your ideas or build your entire app with Sass variables and mixins, responsive grid system, extensive prebuilt components, and powerful plugins built on jQuery.

How to use parsley with Bootstrap Framework

Parsley uses a specific DOM API which allows you to configure pretty much everything directly from your DOM, without writing a single javascript configuration line or custom function. Parsley’s default DOM API is data-parsley-. That means that if in the config you see a foo property, it can be set/modified via DOM with data-parsley- foo=”value”.

We would like to show our validation style like bootstrap native error/success message below:

Learn how Parsley works with Bootstraps to make good forms for your website.

Installation

1. Download Parsley

Parsley relies on jQuery (>= 1.8), and it will need to be included before including Parsley. You will also need to include es5-shim if you want to support IE8.

Then, you can either use parsley.js, unminified file or parsley.min.js minified file. Please download parsley.js from this link.

2. Include Parsley js in HTML form.

Include the Parsley js with jquery.js in the footer before closing the body tag.

< script src=”path-to- jquery-js”></script >
< script src=”path-to- parsley-js”></script >

3. HTML Form

Create Your form with required fields

< form id=””demo-form”” >
< div class=””form-group”” >
< label for=””fullname”” >Full Name * :< /label >
< input class=””form-control”” name=””fullname”” required=”” type=””text”” / >< /div >
< div class=””form-group”” >< label for=””email”” >Email * :< /label >
< input class=””form-control”” name=””email”” required=”” type=””email”” / >< /div >
< div class=””form-group”” >< label for=””password”” >Password * :< /label >
< input class=””form-control”” name=””password”” required=”” type=””password”” / >< /div >
< input class=””btn” type=””submit”” value=””submit”” / >

< /form >

4. Initialise Parsley js

Normally to initialize Parlsey we need to write this after including parsley.js.

< script > $(‘#demo-form’).parsley(); < /script >

Complete Code

< form id=””demo-form”” >
< div class=””form-group”” >
< label for=””fullname”” >Full Name * :< /label>
< input class=””form-control”” name=””fullname”” required=”” type=””text”” / >< /div >
< div class=””form-group”” >< label for=””email””> Email * :< /label >
< input class=””form-control”” name=””email”” required=”” type=””email”” />< /div >
< div class=””form-group”” >< label for=””password”” >Password * :< /label >
< input class=””form-control”” name=””password”” required=”” type=””password”” / ></div > < input class=””btn” type=””submit”” value=””submit”” / >

< /form>
< script > $(‘#demo-form’).parsley(); </script >

To implement Parsley js with Bootstrap Framework we need to do some changes in initializing

< script> $(document).ready(function() {
$(‘#demo-form’).parsley({
trigger: ‘change’,
successClass: “has-success”,
errorClass: “has-error”,
classHandler: function (el) {
return el.$element.closest(‘.form-group’);
},
errorsWrapper: ‘

< p class=”help-block help-block-error” >
< p>’,
errorTemplate: ‘’,
});
});
< /script >

The Complete Code With Changes

< form id=””demo-form”” >
< div class=””form-group”” >
< label for=””fullname””>Full Name * :< /label >
< input class=””form-control”” name=””fullname”” required=”” type=””text”” />< /div >
< div class=””form-group”” >< label for=””email””>Email * :< /label >
< input class=””form-control”” name=””email”” required=”” type=””email”” />< /div >
< div class=””form-group”” >< label for=””password””>Password * :</label >
< input class=””form-control”” name=””password”” required=”” type=””password”” />< /div >
< input class=””btn” type=””submit”” value=””submit”” / >

< /form >< script src=”path-to-jquery-js” >< /script >
< script src=”path-to-parsley-js” >< /script >
< script > $(document).ready(function() {
$(‘#idemo-form’).parsley({
trigger: ‘change’,
successClass: “has-success”,
errorClass: “has-error”,
classHandler: function (el) {
return el.$element.closest(‘.form-group’);
},
errorsWrapper: ‘</p > < p class=”help-block help-block-error” >
< p >’,
errorTemplate: ‘’,
});
}); < /script >

Demo link: http://demo.utkarshshinde.com/parsley-demo/

Conclusion

I have found that Parsley is a great library for form validation with easy application and isn’t time-consuming either. You don’t need much javascript/jquery knowledge either. This makes it easy to use Parsley with the Bootstrap framework. For more information, you can read parsley some documentation here: http://parsleyjs.org/doc/index.html

About Me: My name is Utkarsh Shinde and I have a Bachelors in Engineering (Electrical and Electronics) With 5+ years Experience in HTML5, CSS, JavaScript, PHP, WordPress, Oracle, MySQL Database and have designed many creative and effective working websites.
Website: http://utkarshshinde.com

Contact Us To Start A Job Today!

0/5 (0 Reviews)