While entering your email address and other personal information while signing up for some websites, you usually get the error message that your email address is not valid or password must be a minimum of 12 characters long and much more. Have you ever wondered how it's working? This is possible with the help of form validation. It is an important step to ensure anything entered by the user in the form is in the correct format.
In this article, we'll take a look at how Parsley.js is used for form validation. Let’s begin by first learning about Parsley.js.
What is Parsley.js?
Parsley.js is a JavaScript library for form validation. It is used in web applications to ensure the data entered in a form is in the correct format. It can be used to set various criteria and ensure the data is accurate and meets the requirements set by the developer.
Parsley.js provides a set of built-in validation rules that can be applied to any input fields like email, passwords, checkbox, date, etc. These validation rules are backed by a set of error messages that are used to inform the user about any mistakes while filling out the form.
Let’s discuss all its features in detail.
Features of Parsley.js
The following are the features of Parsley.js:
Built-in Validation rules: Parsley.js contains a large number of predefined rules that can be used for the validation of different input fields. These rules ensure the correct format is being followed.
Customized rules: Along with the built-in rules, users are free to create their own rules to specify their requirements. This makes it more flexible and convenient for developers.
Easy to implement: Parsley.js can be easily added to any web application without the need for any extra knowledge. The CSS and JavaScript files are only needed to be included in the project.
Error messages: In case of violation of the rules, Parsley.js can be used to provide real-time feedback to the user by using the error messages. You can add custom error messages to the form according to the rule.
Validating a Form with Parsley.js
Let’s dive into a real-world example of Form validation. Follow the below-mentioned steps, and you will end up with a Signup form for your website.
Include jQuery and Parsley.js
First, you need to include jQuery, as Parsley.js relies on jQuery (>= 1.8). Both jQuery and Parsley.js can be included in two ways, either using a CDN link or by downloading the files and including it manually.
The CDN links can be included as below in the head section.
You can explicitly customize the error message by including the CSS. Let’s change the color of the error message to red. Just add the following code in the CSS file for the same.
.parsley-errors-list {
color: red;
}
With this, you’re done. Now, the moment of truth.
Output
Let’s try to submit the form with the wrong information and see the results.
Let’s understand the error messages:
Since the name section is empty and we have put a constraint of required, the error message shows Name is required.
The Email ID is not valid as @ is missing in it.
We have set the minimum length constraint in the password, so it should be a minimum of 12 characters long.
Frequently Asked Questions
What is a real-life use of Parsley.js?
Parsley.js is used for validating a form. So, let’s say you have a form to create accounts for new users on your website. It can be used to expose certain restrictions on details entered by users like; passwords must contain a capital character and must be 12 characters long.
Is it difficult to use Parsley.js?
Parsley.js is designed to be user-friendly. You don't need to be a coding expert to use it. The library provides you with simple instructions and tools to make sure your forms are validated without needing to know complex programming.
What is the disadvantage of Parsley.js?
Using Parsley.js can add a bit of overhead to your web page since it requires loading and executing JavaScript code. This could potentially impact the loading speed of your page, especially on slower devices or connections.
Conclusion
In this article, we discussed, in brief, Parsley.js, its features and how it can be used to validate a form in HTML. We have seen the implementation of the same. Hope you have learned something useful through this article.
To learn more about JavaScript, you can read these articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problems, interview experiences, and interview bundles.