Validetta

A tiny jquery plugin for validate forms.

Download Validetta

Currently v1.0.1

Tweet

What is Validetta ?

Validetta is jQuery plugin which you can do client-side validation of your forms. It aims to decrease your burden with easy usage and flexible structure. You can support the repository at GitHub. If have a suggestion or idea, or have found a bug, please create an issue by going to issues page of github repository.

Browser support

The project is tested in Chrome and Firefox. It should work in the current stable releases of Chrome, Firefox, Opera, Safari as well as IE8 and up.

License

Licensed under the MIT License.

What can be done?

How does it work?

It is sufficient to copy downloaded validetta folder to root folder and attach essential folders to your web page. You don’t need to copy validetta folder to root folder but those are applied in the following expression as if validetta is copied to root folder.

include the dependent libraries and css files

<link href="validetta/validetta.css" rel="stylesheet" type="text/css" media="screen" >

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="validetta/validetta.js"></script>

You can include language file if you want.

If you wonder full list of language packages, Check localization folder.

<script type="text/javascript" src="validetta/localization/validettaLang-tr-TR.js"></script>

Then, you will only need to call the plugin inside $(document).ready function:

$("#form").validetta();

and add data-validetta attribute to element which you want to control.

<input type="text" name="exm" data-validetta="required,minLength[2],maxLength[3]" />

Validators

Name Description
required Checks fields whether it is empty or not or it is chosen or not.
number Checks fields if it is consist of number or not.
email Checks email if it is valid or not.
creditCard Checks credit card number written to fields if it is valid or not.
equalTo[input_name] Checks if the two fields are equal to each other according to input name.
different[input_name] new Checks if the two fields are different to each other according to input name.
minLength[x] Checks fields if it is consist of minimal X character.
maxLength[x] Checks maximal X character entry to the area.
minChecked[x] Checks minimal X option if it is marked or not.
maxChecked[x] Checks maximal X option if it is marked or not.
minSelected[x] Checks minimal X option if it is chosen or not.
maxSelected[x] Checks maximal X option if it is chosen or not.
regExp[validator_name] Checks if field is suits to identified ordered expression or not.
remote[validator_name] Checks fields using remote validator.
callback[callback_name] new Return the validity from a callback method

Warning!

Validator arguments ( e.g. input_name, and validator_name ) should be any word character ( letter, number, undercore ) and should not be greater than 15 characters!

Changed validator names

equal validator name has changed to equalTo with v1.0.0

custom validator name has changed to regExp with v1.0.1

Options & Defaults

$('#form').validetta({
  showErrorMessages : true, // If you dont want to display error messages set this option false
  /** You can display errors as inline or bubble */
  display : 'bubble', // bubble or inline
  /**
   * Error template class
   * This is the class which will be added to the error message window template.
   * If you want special style, you can change class name as you like with this option.
   * Error message window template : <span class="errorTemplateClass">Error messages will be here !</span>
   */
  errorTemplateClass : 'validetta-bubble',
  /** Class that would be added on every failing validation field */
  errorClass : 'validetta-error',
  /** Same for valid validation */
  validClass : 'validetta-valid', // Same for valid validation
  bubblePosition: 'right', // Bubble position // right / bottom
  bubbleGapLeft: 15, // Right gap of bubble (px unit)
  bubbleGapTop: 0, // Top gap of bubble (px unit)
  /* To enable real-time form control, set this option true. */
  realTime : false,
  validators: {} // Custom validators stored in this variable
  /** Callback functions */
  onValid : function(){},
  onError : function(){}
});

Removed options!

ajax and onCompleteFunc options has removed with v1.0.0

Data Attribute Settings

Attribute Description
data-vd-parent-up="{count}" This feature allows you to change the element which to be added error messages. When you working with customized inputs or selectboxes this feature will be beneficial to you. Check the example
data-vd-message-[type]="Custom error message" This allows you to change error messages via data attribute. Check the example

Callbacks

There are two different callbacks you can declare when setting up the validation.

$('#form').validetta({
  onValid : function( event ) {
    event.preventDefault(); // Will prevent the submission of the form
    alert( 'Nice, Form is valid.' );
  },
  onError : function( event ){
    alert( 'Stop bro !! There are some errors.');
  }
});

Getting invalid fields

Returns the list of invalid fields as an object, containing the properties "field" and "errors".

$('#form').validetta({
  onError : function( event ){
    // this -> form object
    var invalidFields = this.getInvalidFields();
  }
});

Remote Validation

This option allows you to perform server-side validation. By using this option you can validate the value given by the user on server side before the form gets submitted. The validation function will send an ajax request to URL declered in remote options.

The form will get the class validetta-pending while the server is being requested.

The response from the ajax request

The response from the ajax request must be a JSON formatted object, containing the properties "valid" and "message".

Remote validator settings

$('#form').validetta({
  remote : {
    validator_name : {
      // Here, you must use ajax setting determined by jQuery
      // More info : http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings
      type : 'POST',
      url : 'remote.php',
      datatype : 'json'
    }
  }
});

An example for remote.php

<?php
$response = array( 'valid' => false, 'message' => 'Sorry, Something went wrong!');
if( isset($_POST['username']) ) {

  if ( $_POST['username'] !== 'validetta' ) {
  $response = array( 'valid' => false, 'message' => 'This username is already taken!' );
  } else {
  $response = array( 'valid' => true );
  }

}
echo json_encode($response);;

Information!

Remote validation request fires if does not have any client-side error in the field!

Override global error messages

Sometimes, you may want to edit error messages, instead of including the language file. Here you can find the list of all built in error messages.

$('#form').validetta({
  realTime: true
},
{
  required  : 'Custom error message.'
});

Scss Variables

Name Description
$validetta-font-size Font size of the error message
$validetta-line-height Line height of the error message
$validetta-font-family Font family of the error message
$validetta-bubble-color Font color of the error message in the bubble type
$validetta-bubble-bg-color Background color of the error message in the bubble type
$validetta-bubble-radius Border radius of the error message in the bubble type
$validetta-bubble-caret-size Caret size of the error message in the bubble type
$validetta-bubble-pad-vertical Vertical padding of the error message in the bubble type
$validetta-bubble-pad-horizontal Horizontal padding of the error message in the bubble type
$validetta-inline-color Font color of the error message in the inline type

Examples