Validate Form with Forms Validation Library – Parsley.js

Parsley.js is a JavaScript library that provides unobtrusive form validation. It makes it easy to validate user input on the client-side, before submitting the form to the server. This can help to improve the user experience by preventing them from submitting invalid data.

Basic Setup

To use Parsley.js, you first need to include the Parsley.js library in your HTML page. You can do this by downloading the library from the Parsley.js website and linking to it in your HTML file, or by using a CDN (content delivery network) such ascdnjs.

HTML
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.8.0/parsley.min.js"></script>

Once you have included the Parsley.js library, you can start using it to validate your forms. Parsley.js uses data attributes to define the validation rules for your form fields. For example, to make a field required, you can add the data-parsley-required attribute to the field.

HTML
<inputtype="text"name="name"data-parsley-required>

Parsley.js also provides a number of other validators, such as data-parsley-email, data-parsley-length, and data-parsley-min. You can find a complete list of validators in the Parsley.js documentation.

Custom Validation

In addition to the built-in validators, Parsley.js also allows you to create your own custom validators. This is useful for validating complex data that cannot be validated with the built-in validators.

To create a custom validator, you first need to define a function that returns true if the value is valid, and false if it is not. You can then add this function to Parsley.js using the addValidator method.

JavaScript
Parsley.addValidator('myValidator', function(value) {
// Validate the value here
return isValid;
});

Once you have defined your custom validator, you can use it to validate a form field by adding the data-parsley-myValidator attribute to the field.

Error Messages

Parsley.js automatically displays error messages when a field is not valid. You can customize the error messages by adding the data-parsley-error-message attribute to the field.

HTML
<inputtype="text"name="name"data-parsley-requireddata-parsley-error-message="Please enter a name">

You can also use the data-parsley-errors-container attribute to specify the DOM element where the error messages should be displayed.

Submitting Forms

When a form is submitted, Parsley.js will validate all of the form fields. If any of the fields are not valid, the form will not be submitted, and error messages will be displayed. To prevent the form from submitting, Parsley.js will return false from the onSubmit event handler.

JavaScript
$('#form').on('submit', function(event) {
if (!$('#form').parsley().validate()) {
event.preventDefault();
}
});

Advanced Use Cases

Parsley.js also provides a number of advanced features, such as:

  • Multi-step forms
  • Remote validation
  • Custom themes

For more information on these features, please refer to the Parsley.js documentation.

Leave a Reply