Convert Figma logo to code with AI

guillaumepotier logoParsley.js

Validate your forms, frontend, without writing a single line of javascript

9,026
1,302
9,026
72

Top Related Projects

jQuery Validation Plugin library sources

String validation

21,115

The most powerful data validation library for JS

Quick Overview

Parsley.js is a popular JavaScript form validation library. It provides an easy-to-use, extensible solution for validating user input in web forms, offering both client-side and server-side validation capabilities.

Pros

  • Easy to implement with minimal setup required
  • Highly customizable with a wide range of built-in validators
  • Supports multiple languages and localization
  • Lightweight and performant

Cons

  • Requires jQuery as a dependency (though a standalone version is available)
  • Some users report occasional issues with complex form structures
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Basic form validation:
<form id="demo-form" data-parsley-validate>
  <input type="text" name="name" required data-parsley-length="[2, 20]">
  <input type="email" name="email" required>
  <input type="submit" value="Submit">
</form>

<script>
$('#demo-form').parsley();
</script>
  1. Custom validator:
window.Parsley.addValidator('multipleOf', {
  validateString: function(value, requirement) {
    return value % requirement === 0;
  },
  messages: {
    en: 'This value should be a multiple of %s'
  }
});
  1. Asynchronous validation:
window.Parsley.addAsyncValidator('checkUsername', function(xhr) {
  return xhr.responseJSON.available;
}, '/check-username');

<input type="text" name="username" data-parsley-check-username>

Getting Started

  1. Include Parsley.js in your project:
<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.9.2/parsley.min.js"></script>
  1. Add data-parsley-validate to your form and appropriate validation attributes to your inputs:
<form data-parsley-validate>
  <input type="text" required data-parsley-length="[5, 20]">
  <input type="email" required>
  <input type="submit" value="Submit">
</form>
  1. Initialize Parsley on your form:
$(document).ready(function() {
  $('form').parsley();
});

Competitor Comparisons

jQuery Validation Plugin library sources

Pros of jQuery Validation

  • Extensive documentation and community support
  • Large set of built-in validation methods
  • Seamless integration with jQuery

Cons of jQuery Validation

  • Dependency on jQuery library
  • Heavier footprint compared to Parsley.js
  • Less flexible for custom validation rules

Code Comparison

jQuery Validation:

$("#myform").validate({
  rules: {
    name: "required",
    email: {
      required: true,
      email: true
    }
  }
});

Parsley.js:

$('#myform').parsley({
  trigger: 'change',
  errorClass: 'error',
  successClass: 'success',
  classHandler: function(el) {
    return el.$element.closest('.form-group');
  }
});

Both libraries offer form validation capabilities, but they differ in implementation and features. jQuery Validation is more tightly integrated with jQuery and provides a wide range of pre-built validation methods. It's well-documented and has strong community support, making it easier for developers familiar with jQuery to adopt.

On the other hand, Parsley.js is more lightweight and flexible. It doesn't require jQuery (although it can be used with it) and allows for easier customization of validation rules and error messages. Parsley.js also supports asynchronous validation out of the box, which can be beneficial for more complex validation scenarios.

The code comparison shows that jQuery Validation uses a more declarative approach, defining rules within the plugin initialization. Parsley.js, however, relies more on HTML5 data attributes for defining validation rules, with additional configuration options available during initialization.

String validation

Pros of validator.js

  • Lightweight and focused on string validation
  • Supports both server-side (Node.js) and client-side validation
  • Extensive set of built-in validation methods

Cons of validator.js

  • Lacks form-specific features and UI integration
  • Requires more manual setup for form validation scenarios
  • No built-in localization support

Code Comparison

validator.js:

import validator from 'validator';

const isValid = validator.isEmail('example@email.com');
console.log(isValid); // true

Parsley.js:

$('#form').parsley().on('form:submit', function() {
  // Form is valid
  return false;
});

Key Differences

  • Parsley.js is specifically designed for form validation, while validator.js is a more general-purpose string validation library
  • Parsley.js provides a declarative approach using HTML attributes, whereas validator.js requires manual JavaScript implementation
  • Parsley.js offers built-in UI feedback and error messaging, which validator.js does not provide out of the box

Use Cases

  • Choose validator.js for flexible string validation in various contexts, including server-side validation
  • Opt for Parsley.js when focusing on client-side form validation with minimal setup and built-in UI integration

Both libraries have their strengths, and the choice depends on the specific requirements of your project and whether you need a form-centric or more general validation solution.

21,115

The most powerful data validation library for JS

Pros of Joi

  • More comprehensive validation capabilities, including complex object and array validation
  • Server-side validation, making it suitable for API and backend development
  • Extensive documentation and active community support

Cons of Joi

  • Steeper learning curve due to its extensive feature set
  • Primarily designed for Node.js, limiting its use in client-side applications
  • Larger bundle size compared to Parsley.js

Code Comparison

Joi validation example:

const schema = Joi.object({
  username: Joi.string().alphanum().min(3).max(30).required(),
  email: Joi.string().email().required()
});

const { error, value } = schema.validate({ username: 'john_doe', email: 'john@example.com' });

Parsley.js validation example:

$('#form').parsley().validate();

<input type="text" name="username" required data-parsley-minlength="3" data-parsley-maxlength="30">
<input type="email" name="email" required>

Summary

Joi is a powerful server-side validation library for Node.js, offering extensive validation capabilities and robust documentation. It excels in complex object validation scenarios but has a steeper learning curve. Parsley.js, on the other hand, is a lightweight client-side validation library that integrates easily with HTML forms and provides a simpler API for basic form validation tasks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Parsley

Build Status

JavaScript form validation, without actually writing a single line of JavaScript!

Maintenance status

This project is considered stable, no new features are planned.

Minimal maintenance by @marcandre. Good quality PRs fixing bugs will be merged. Enquire before working on new features.

Version

2.9.2

Doc

See index.html and doc/

Requirements

jQuery >= 1.8 (compatible with 2.x and 3.0) es5-shim if you want need to support IE8

Questions?

Please ask questions on StackOverflow and be sure to include the parsley.js tag. Please provide an example, starting for example from this jsfiddle

Contributing

See the CONTRIBUTING.md file

Integrations

Create integration with other framework as a separate Github repo and send a pull request for including here. Some integrations are

Install dev environment and running tests

First time: install npm and:

npm install -g gulp

then

npm install
gulp test

Build dist/ and doc/annotated-source

gulp build

Run tests

In the browser: run a server with gulp test-browser, then open test/runner.html

In the terminal: gulp test

License

Released under the MIT License. See the bundled LICENSE file for details.

NPM DownloadsLast 30 Days