Laravel style jquery validation plugin

Overview

Validator

Laravel style jquery validation plugin

Validator is a jQuery plugin that emulates the validation class found in the laravel framework.

Usage

The validator plugin will return an instance of the validator object.

Basic Validation Example

var contactForm = $('form').validator({
    'name' : 'required|max:12'
});

Without a jQuery collection

When a jQuery collection is not being used as the data source, you can use an object as the first argument as the data source.

var contactForm = $.validator(
    {'name' : 'Alice'},
    {'name' : 'required|max:12'}
);

The fails and passes functions are used to perform the validation once a Validator object has been created. These methods take an anonymous function as there only arguments which will be called once the validation occurs.

contactForm.fails(function( messages ){
     // The given data did not pass validation
});

The context of these functions is the data source. They also returns a Boolean value.

if(contactForm.fails()){
     // The given data did not pass validation
}

Error Messages

all() Retrives all the messages on all fields

allElements() Retrives all the elements with messages

get( String field ) Retrives all the messages on a particular field

any() returns true if any fields have any messages

count() returns the number of messages over all the fields

first( String field ) gets the first message on a particular element

has( String field ) checks if a given field has any messages

Custom Messages

A JSON array of custom error messages can be passed as a third argument. Occurances of :attribute will be replaced with the field name as well as rule parameters when applicable.

var contactForm = $(this).validator(
    {'name'    :'required|between:1,10|alpha_dash'},
    {'between' :':attribute must be between :min and :max'}
);

Specifying A Custom Message For A Given Attribute

var contactForm = $(this).validator(
    {'name'         :'required|between:1,10|alpha_dash'},
    {'name.between' :':attribute must be between :min and :max'}
);

Validation Rules

Most of the Validation rules in the laravel framework are also avaliable in this plugin, with the exceptions of Active URL, Date Format, Exists (Database), Image (File), MIME Types and Unique (Database). You can find more information on the validation rules in the laravel documentation

  • After (Date) after:date
  • alpha_dash alpha
  • Alpha Dash alpha_dash
  • Alpha Numericalpha_num
  • Before (Date)before:date
  • Between between:min,max
  • Confirmed confirmed
  • Date date
  • Different different:field
  • E-Mail email
  • In in:foo,bar...
  • Integer Integer
  • IP Address ip
  • Max max:value
  • Min min:value
  • Not In not_in:foo,bar...
  • Numeric numeric
  • Regular Expressionregex:pattern
  • Required equired
  • Required If required_if:field,value
  • Required With required_with:foo,bar...
  • Required Without required_without:foo,bar...
  • Same same:field
  • Size size:value
  • URL url

Custom Rules

The extend function takes three arguments. the final argument is an array of message placeholders. The rule parameters are mapped onto this array. In the example below, occurances of :foo get replaced with the value of parameters[0]

contactForm.extend('myRule', function(attribute, value, parameters){
     // Return Boolean
}, [':foo', ':bar']);

Example

The following toggles the error class on fields with errors and submits the form once it has been successfully validated.

$('form').submit(function(e){
    e.preventDefault();

    $('form input').removeClass('error');
    
    var contactForm = $(this).validator({
        'name' :'required|max:12',
        'email' : 'required|email'
    });

    contactForm.fails(function( messages ){
        messages.allElements().addClass('error');
    });

    contactForm.passes(function(){
        $(this).submit();
    });
});

This repo was initially forked from Tom-Alexander

You might also like...
Strongly typed settings for Laravel, includes built-in encryption and friendly validation.

Strongly Typed Laravel Settings Install composer require bogdankharchenko/typed-laravel-settings Model Setup namespace App\Models\User; use Illuminat

Email validation service in PHP

Email validation service Email validation service in PHP using Laravel 8. Validation features Domain Regular Expression Smtp Txt records Installing de

Proxy validation or Proxy checker. Command line version
Proxy validation or Proxy checker. Command line version

Proxy Checker Proxy validation or Proxy checker Install on desktop : Install XAMPP Added environment variable system path = C:\xampp\php download the

Enjoy realtime input validation by passing your rules in your input itself.

Laravel Realtime input Enjoy realtime input validation by passing your rules in your input itself. Requirments This package is tested with Laravel v8

Simple solution for form request validation on lumen.

Form Request Validation for Lumen This small package contains a laravel-like solution for request form validation. Contents Form Request Validation fo

If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin
If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

hirwa-products-plugin If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Impersonate is a plugin that allows you to authenticate as your users.

Laravel Impersonate Laravel Impersonate makes it easy to authenticate as your users. Add a simple trait to your user model and impersonate as one of y

A Laravel Larex plugin to import/export localization strings from/to Crowdin
A Laravel Larex plugin to import/export localization strings from/to Crowdin

Laravel Larex: Crowdin Plugin A Laravel Larex plugin to import/export localization strings from/to Crowdin 📋 Requirements PHP 7.4 | 8.0 Laravel ≥ 7 L

Owner
David Thingsaker
David Thingsaker
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.

QueryBuilderParser Status Label Status Value Build Insights Code Climate Test Coverage QueryBuilderParser is designed mainly to be used inside Laravel

Tim Groeneveld 149 Nov 11, 2022
Server-side handler of DataTables Jquery Plugin for Laravel 4

Project is not being maintained actively. You will most likely find a better more actively maintained fork here https://github.com/yajra/laravel-datat

Bilal Gultekin 264 Jul 2, 2022
jQuery DataTables API for Laravel 4|5|6|7|8

jQuery DataTables API for Laravel 4|5|6|7|8 This package is created to handle server-side works of DataTables jQuery Plugin via AJAX option by using E

Arjay Angeles 4.5k Jan 9, 2023
Run patches migration style in your Laravel applications.

This package generates patch files in the same fashion Laravel generates migrations. Each file is timestamped with an up and a down method and is asso

Anthony Rappa 44 Sep 9, 2022
Let's base your Laravel project with PROS style

Purpose This library is for convenient methods that use to register code base for Laravel project Target We aimed to reduce complexity for real projec

Protean Studios Co., Ltd. 4 Mar 29, 2022
Livewire UI components with tailwind base style

WireUI ?? Documentation Wire UI is a library of components and resources to empower your Laravel and Livewire application development. Starting a new

WireUi 811 Jan 2, 2023
A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr.

PHP Paginator A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The "first" and "last" page l

Jason Grimes 370 Dec 21, 2022
A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache.

Laravel OTP Introduction A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache. The ca

Lim Teck Wei 52 Sep 6, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
🛂 Use this package to validate the identity card from your country using laravel validation rules.

Identity Card Checker Laravel Validation Rules Use this package to validate the identity card number from your country Installation You can install th

David Torralbo Pérez 13 Feb 8, 2022