Get started

Demo

Options & Rules

JudgementJS

JudgementJS is a small vanilla JavaScipt plugin that will accelerate your development of form validation on the client side. Inspired by the "Form_Validation" class of the PHP CodeIgniter framework, JudgementJS is based on a system of validation rules that you define for your fields of your form which requires validation.

JudgementJS has been designed to allow developers to write as little code as possible and thus increase productivity. If the javascipt is not your cup of tea or you do not want to get yourself into a tedious development, JudgementJS is for you.

You also have the ability to internationalize and customize the error messages of your forms using the JSON language files.

All this and much more can be achieved in just a few lines of code. TRY IT!

Installation

Standalone

Download latest release

Include the plugin file into your page.

<script src="judgement.min.js"></script>

npm

In your terminal run

npm install judgementjs

Use in node projects

var Judgement = required('judgementjs');

Please note that you can't use JudgementJS on the server side because the plugin uses the document object and that object is unknown for node.js, so JudgementJS works only on client side.

Bower

In your terminal run

bower install judgementjs

Include the plugin into your page

<script src="bower_components/judgementjs/judgement.min.js"></script>

Usage

To use "judgementJs" follow the steps below.

  1. Create a form.
  2. Add some inputs with id, data-rules and data-label-name attributs.
    • data-rules attribute must contain the validation rules separated by pipes | (list of rules).
    • data-label-name attribute must contain the name of the input label. The lable name is used in the error messages.
  3. Add a submit button with an id attribute. (The button can be any valid html tag, but in general it is the <input>, <button> or <a> tag).
  4. Write some lines of JavaScript.

HTML

<form id="my-form">
	<label>My input</label>
	<input type="text" id="my-input" data-rules="required|alpha_numeric|min_length[5]" data-label-name="My input" />
	
	<a id="my-submit-button">My button</a>
</form>

JS

var options = {
	formId : 'my-form',
	submitButtonId : 'my-submit-button'
};
var myForm = new Judgement(options);
myForm.startJudgement();

That's all. Now your form is checked in real time.

The formId and submitButtonId options are required, and you can choose several other options. For instance, you can prevent/allow submitting form with Enter key with preventEnterKey option, enable/disable the input highlights (red/green borders) with inputHighlights option or enable/disable the test mode with testMode option to never submit the from.

JS

var options = {
	formId : 'my-form',
	submitButtonId : 'my-submit-button',
	onBlurChecking : true,
	preventEnterKey: true,
	inputHighlights: false,
	testMode: true
};
var myForm = new Judgement(options);
myForm.startJudgement();

Easy !

For more options please check the list of options

Example

I agree to the Company Terms of Service and Privacy Policy

Judge

Html

<form id="example-form">
	<label>Name</label>
	<input type="text" id="example-name" data-rules="required|alpha_dash_space" data-label-name="Name" />
	
	<label>Email</label>
	<input type="text" id="example-email" data-rules="required|email" data-label-name="Email" />
	
	<label>Confirm email</label>
	<input type="text" id="example-confirm-email" data-rules="equal_to[example-email,Email]" data-label-name="Confirm Email" />
	
	<label>Birth date</label>
	<input type="text" id="example-date" data-rules="required|date[ddmmyyyy,.]" data-label-name="Birth date" />
	
	<label>Terms of Service</label>
	<input type="checkbox" id="example-tos" data-rules="required" data-label-name="Terms of Service" /> I agree to the Company <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a> <br/><br/>
	
	<a id="example-submit-button" class="btn">Judge</a>
</form>

JS

var options = {
	formId : 'example-form',
	submitButtonId : 'example-submit-button',
	onBlurChecking : true,
	onSubmit: function(form){
		alert("SUCCESS !");
	}
};
var exampleForm = new Judgement(options);
exampleForm.startJudgement();

Errors

JudgementJS offers several options for managing and displaying validation errors.

  1. Show errors individually over each erroneous field.

    For each error, a span element is created containing the error message and the error message is placed before the field.

    The span looks like this.

    <span class="fv-error" id="fv-error-field-id">Some error message</span>

    The fv-error class allows you to apply a style to errors and the id is used by the plugin for error handling.

    Individual errors are triggered by realTimeChecking and onBlurChecking options.

  2. Display all errors in a container.

    The errorContainer option allows you to display errors in one place on the page. To do this, just give it the id of a container that is on the page. E.g.

    var options = {
    	...
    	errorContainer : 'my-error-container-id'
    }
    var form = new Judgement(options);
    form.startJudgement();

    The form checking starts when the submit button is clicked.

  3. Manually manage the error displaying.

    If the above two methods are not suitable for you, it is possible to manage the error display manually with the onError and onSuccess. E.g.

    var options = {
    	...
    	onError : function(errorsFields) {
    		console.log(errorsFields);
    		
    		// your custom code to display errors.
    	},
    	onSuccess : function(successesFields, errorsFields){
    		console.log(successesFields);
    		
    		// your custom code to display successes and hide some error messages.
    	}
    }
    var form = new Judgement(options);
    form.startJudgement();

    The form checking starts when the submit button is clicked.

    In addition to error handling, you must also manually manage the highlighting of fields.

Errors / Inputs Styles

Only three classes are required to style your error messages and input highlights

  1. Error messages .j-error
    .j-error{
    	margin: 0;
    	padding: 0;
    	color: #ff0000;
    	display: block;
    }
  2. Error highlight .j-error-highlight
    .j-error-highlight{
    	border-top: 1px dotted rgba(237, 18, 18, .5) !important;
    	border-right: 1px dotted rgba(237, 18, 18, .5) !important;
    	border-bottom: 2px solid #ED1212 !important;
    	border-left: 1px dotted rgba(237, 18, 18, .5) !important;
    }
  3. Success highlight .j-success-highlight
    .j-success-highlight{
    	border-color: #0FDB4C;
    	border-top: 1px dotted rgba(15, 219, 76, .5) !important;
    	border-right: 1px dotted rgba(15, 219, 76, .5) !important;
    	border-bottom: 2px solid #0FDB4C !important;
    	border-left: 1px dotted rgba(15, 219, 76, .5) !important;
    }

!important tag is used to override the defaul input style.

Internationalization

The jsonLangFile option allows you to load a language file containing the error messages.

Please note that jsonLangFile option will work with server environment only. Offline testing on your local PC won't work. You must install MAMP or AMPPS or something similar to test locally. This only applies if the jsonLangFile option is set, otherwise you don't need a server environment to test the plugin.

var options = {
	formId : 'lang-form',
	submitButtonId : 'lang-submit-button',
	onBlurChecking : true,
	jsonLangFile : 'js/langs/fr.json'
};
var langForm = new Judgement(options);
langForm.startJudgement();

The following form displays now the error messages in french.

Judge

The Json file

That file contains error messages that the plugin displays for each rule. E.g.

{
	...
	"range_length" : "{field} requires between {min} and {max} characters"
	...
}

The messages contains different tags that you must keep and don't change their names. E.g. If you need the range_rule you will write range_rule[5,10] so the {min} will be replaced by 5 ant the {max} will be replaced by 10. The tag {field} will be replaced by the value of the data-label-name attribute.

JudgementJS comes with two language files en.json and fr.json. If you not set the jsonLangFile the default language is used (English).

required

Checks if a value is provided.

Valid

Html

<input type="text" id="anyId" data-rules="required" data-label-name="Required" />

required_one_of

Check if at least one checkbox or radio button is checked in a group.

Bentley Mercedes-Benz Porsche BMW

Valid

Html

<input type="text" id="car1" name="cars" data-rules="required_one_of" data-label-name="Required one of" />Bentley
<input type="text" id="car2" name="cars" data-rules="required_one_of" />Mercedes-Benz
<input type="text" id="car3" name="cars" data-rules="required_one_of" />Porsche
<input type="text" id="car4" name="cars" data-rules="required_one_of" />BMW

alpha

Checks if is alphabetic characters only.

Judge

Html

<input type="text" id="anyId" data-rules="alpha" data-label-name="Alpha" />

alpha_numeric

Checks if is alphabetic and numeric characters only.

Judge

Html

<input type="text" id="anyId" data-rules="alpha_numeric" data-label-name="Alpha Numeric" />

alpha_dash

Checks if is alphabetic, numeric, underscores and dashes characters only.

Judge

Html

<input type="text" id="anyId" data-rules="alpha_dash" data-label-name="Alpha Dash" />

alpha_space

Checks if is alphabetic characters and spaces only.

Judge

Html

<input type="text" id="anyId" data-rules="alpha_space" data-label-name="Alpha Space" />

alpha_numeric_space

Checks if is alphabetic, numeric and spaces characters only.

Judge

Html

<input type="text" id="anyId" data-rules="alpha_numeric_space" data-label-name="Alpha Numeric Space" />

alpha_dash_space

Checks if is alphabetic, numeric, underscores, dashes and spaces characters only.

Judge

Html

<input type="text" id="anyId" data-rules="alpha_dash_space" data-label-name="Alpha Dash Space" />

numeric

Checks if is numeric characters only.

Judge

Html

<input type="text" id="anyId" data-rules="numeric" data-label-name="Numeric" />

integer

Checks if is integer only. (Positive or negative)

Judge

Html

<input type="text" id="anyId" data-rules="integer" data-label-name="Integer" />

decimal

Checks if is decimal only. (Positive or negative)

Judge

Html

<input type="text" id="anyId" data-rules="decimal" data-label-name="Decimal" />

natural

Checks if is natural numbers only. (Positive only)

Judge

Html

<input type="text" id="anyId" data-rules="natural" data-label-name="Natural" />

natural_no_zero

Checks if is natural numbers only, without zero. (Positive only)

Judge

Html

<input type="text" id="anyId" data-rules="natural_no_zero" data-label-name="Natural No Zero" />

email

Checks if is valid email address.

Judge

Html

<input type="text" id="anyId" data-rules="email" data-label-name="Email" />

url

Checks if is valid url address.

Judge

Html

<input type="text" id="anyId" data-rules="url" data-label-name="Url" />

ip_address

Checks if is valid IP address.

Judge

Html

<input type="text" id="anyId" data-rules="ip_address" data-label-name="IP Address" />

base64

Checks if is base 64 characters only.

Judge

Html

<input type="text" id="anyId" data-rules="base64" data-label-name="Base 64" />

password

Checks if is alphabetic, numeric and special (@,$,!,%,*,?,&) characters only. And 6 characters at least, 1 Uppercase character, 1 lowercase character and 1 numeric character.

Judge

Html

<input type="text" id="anyId" data-rules="password" data-label-name="Password" />

credit_card

Checks if is valid credit card number. For Visa, MasterCard, American Express, Diners Club, Discover or JCB cards.

Judge

Html

<input type="text" id="anyId" data-rules="credit_card" data-label-name="Credit Card" />

time

Checks if is valid time value. (hh:mm:ss)

Judge

Html

<input type="text" id="anyId" data-rules="time" data-label-name="Time" />

date

Check if is valid date for n format.

Judge

Html

<input type="text" id="anyId" data-rules="date[ddmmyyyy,.]" data-label-name="Date" />

min_length

Check for a minimum length of n characters.

Judge

Html

<input type="text" id="anyId" data-rules="min_length[8]" data-label-name="Min Length" />

max_length

Check for a maximum length of n characters.

Judge

Html

<input type="text" id="anyId" data-rules="max_length[15]" data-label-name="Max Length" />

exact_length

Check for a exact length of n characters.

Judge

Html

<input type="text" id="anyId" data-rules="exact_length[4]" data-label-name="Exact Length" />

range_length

Check for a minimum length of x and a maximum length of y characters.

Judge

Html

<input type="text" id="anyId" data-rules="range_length[5,10]" data-label-name="Range Length" />

greater_than

Check if value is greater than n parameter.

Judge

Html

<input type="text" id="anyId" data-rules="greater_than[100]" data-label-name="Greater Than" />

greater_than_equal_to

Check if value is greater or equal than n parameter.

Judge

Html

<input type="text" id="anyId" data-rules="greater_than_equal_to[25]" data-label-name="Greater Than Equal To" />

less_than

Check if value is less than n parameter.

Judge

Html

<input type="text" id="anyId" data-rules="less_than[100]" data-label-name="Less Than" />

less_than_equal_to

Check if value is less or equal than n parameter.

Judge

Html

<input type="text" id="anyId" data-rules="less_than_equal_to[25]" data-label-name="Less Than Equal To" />

in_list

Check if value is in the n list.

Judge

Html

<input type="text" id="anyId" data-rules="in_list[red,blue,green]" data-label-name="In List" />

equal_to

Check for equality between two values.

Judge

Html

<input type="text" id="anyId" data-rules="equal_to[password,Password]" data-label-name="Confirm Password" />

different_from

Check for inequality between two values.

Judge

Html

<input type="text" id="anyId" data-rules="different_from[id-1,ID 1]" data-label-name="ID 2" />

Options

Name Type Default Accepted Desctiption
formId * string null any string The id of the form which you want to check.
submitButtonId * string null any string The id of the submit button of your form.
realTimeChecking  boolean false true or false Enables/Disables the real time field checking.
onBlurChecking  boolean true true or false Enables/Disables the on blur field checking.
errorContainer  string null any string The id of the DOM element where you want display the form errors.
inputHighlights boolean true true or false Enables/Disables the inputs highlights. If has error the input borders are red and if has success the input borders are green.
preventEnterKey boolean false true or false If its set to true, prevents the form submission with the Enter key.
jsonLangFile string null any valid path Allows you to load a JSON language file which contains the error messages of the form.
testMode boolean false true or false Checks the form and prevent its submission with a submit button or Enter key.

* required options.

realTimeChecking, onBlurChecking and errorContainer can not be used at the same time. You must to choose one of them.

Callback functions

Name Type Default Accepted Desctiption
onSubmit function null Any valid function() Runs when the form is correctly filled. Allows to manipulate and send form manually.
onSuccess function null Any valid function() Runs when the form has successes fields. Allows to manipulate successes fields manually.
onError function null Any valid function() Runs when the form has errors. Allows to manipulate errors manually.

Note: The onSubmit callback function can manipulate the form through the callback parameter (e.g "form").
The onError callback function can manipulate the errors through the callback parameter (e.g. "errors").

Rules

Rule Parameter Description Example
required No Returns FALSE if the form element is empty. -
required_one_of No Return FALSE as long as a checkbox or a radio button in a group is not checked. -
alpha No Returns FALSE if the form element contains anything other than alphabetical characters. -
alpha_numeric No Returns FALSE if the form element contains anything other than alpha-numeric characters. -
alpha_dash No Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes. -
alpha_space No Returns FALSE if the form element contains anything other than alphabetical characters or spaces. -
alpha_numeric_space No Returns FALSE if the form element contains anything other than alpha-numeric characters or spaces. -
alpha_dash_space No Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores, dashes or spaces. -
numeric No Returns FALSE if the form element contains anything other than numeric characters. -
integer No Returns FALSE if the form element contains anything other than an integer. -
decimal No Returns FALSE if the form element contains anything other than a decimal number. -
natural No Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. -
natural_no_zero No Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc. -
email No Returns FALSE if the form element does not contain a valid email address. -
url No Returns FALSE if the form element does not contain a valid URL. -
ip_address No Returns FALSE if the supplied IP address is not valid. Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format. -
base64 No Returns FALSE if the supplied string contains anything other than valid Base64 characters. -
password No Return FALSE if the supplied string contains anything other than minimum 6 characters at least 1 uppercase alphabet, 1 lowercase alphabet, 1 number and 1 special character ($,@,$,!,%,*,?,&) -
credit_card No Returns FALSE if the form element does not contain a valid credit card number. Accepted cards: Visa, MasterCard, American Express, Diners Club, Discover and JCB. -
time No Returns FALSE if the form element does not contain a valid time. Format: hh:mm:ss -
date Yes Returns FALSE if the form element does not contain a valid Date. Rule details : date[format,separator]
Accepted formats : ddmmyyyy or yyyymmdd
Accepted separators : . or - or /
date[ddmmyyyy,.]
min_length Yes Returns FALSE if the length of the form element value is shorter than the parameter value. min_length[5]
max_length Yes Returns FALSE if the length of the form element value is longer than the parameter value. max_length[10]
exact_length Yes Returns FALSE if the length of the form element value is not exactly the parameter value. exact_length[3]
range_length Yes Returns FALSE if the length of the form element value is not between the parameter values. range_length[5,10]
greater_than Yes Returns FALSE if the form element is less than or equal to the parameter value or not numeric. greater_than[25]
greater_than_equal_to Yes Returns FALSE if the form element is less than the parameter value, or not numeric. greater_than_equal_to[25]
less_than Yes Returns FALSE if the form element is greater than or equal to the parameter value or not numeric. less_than[25]
less_than_equal_to Yes Returns FALSE if the form element is greater than the parameter value, or not numeric. less_than_equal_to[25]
in_list Yes Returns FALSE if the form element is not within a predetermined list. in_list[red,blue,green]
equal_to Yes Returns FALSE if the form element does not match the one in the parameter. equal_to[field_id,field_label]
different_from Yes Returns FALSE if the form element does not differ from the one in the parameter. different_from[fieldId,field_label]