Skip to content
troyji edited this page Sep 27, 2011 · 4 revisions

jQuery.regula

Overview

Regula includes an optional plugin for jQuery. To use it, simply include the ‘jquery.regula.js’ file in addition to the Regula and jQuery libraries.

Usage

Add the scripts:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="regula.js"></script>
<script type="text/javascript" src="jquery.regula.js"></script>

Call a regula method:

//Call a regula method on the jQuery object
$.regula('method', [options]);
//call a regula method on a jQuery selection
$(selector).regula('method', [options]);

Regula jQuery methods are chainable:

//chaining without a selector
$.regula('bind').regula('validate');
//chaining with a selector
$(selector).regula('bind').regula('validate'); 

Basic Javascript Example

Bind all input fields and validate them:

$.regula('bind');
var results = $.regula('validate');
$.each(results, function(i, r)
{
  alert(r.message);
});

Functionality

General

It is often useful to select only elements with data-constraints when working with Regula.

var constrainedElements = $("[data-constraints]");

Bind

//bind all elements with constraints
$.regula('bind'); 
//bind all elements with constraints using options
$.regula('bind', options); 
//bind selected elements
$(selector).regula('bind'); 
//bind selected elements using options
$(selector).regula('bind', options); 
  • When binding a jQuery selection, omit the ‘elementId’ and ‘elements’ options. The selection will automatically be supplied to the ‘elements’ option.

Validate

//validate all bound elements
$.regula('validate'); 
//validate all bound elements using options
$.regula('validate', options); 
//validate selected elements
$(selector).regula('validate'); 
//validate selected elements using options
$(selector).regula('validate', options); 
  • When validating a jQuery selection, omit the ‘elementId’ and ‘elements’ options. The selection will automatically be supplied to the ‘elements’ option.
  • The validate method is not chainable like other methods. Once you call validate in a chain, the validation results are returned.
  • The validate method can only validate against constrained elements. When calling validate on a jQuery selection, be sure the selection only contains constrained elements. You can achieve this by filtering the selection by an existing ‘data-constraints’ attribute.

Custom

//create a new custom constraint type
$.regula('custom', options); 

Compound

//create a new custom constraint type based on existing constraints
$.regula('compound', options); 

Override

//override an existing constraint type
$.regula('override', options); 

Example

example.html

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="regula.js"></script>
    <script type="text/javascript" src="example.js"></script>
  </head>
  <body>
    <input type="text" data-constraints="@IsNumeric"></input>
    <button id="validate">Validate</button>
  </body>
</html>

example.js

$(window).load(
  function()
  {
    $("#validate").click(function()
    {
      $.regula('bind');
      var results = $.regula('validate');
      $.each(results, function(i, r)
      {
        alert(r.message);
      });
    });
  });
Clone this wiki locally