How the unobtrusive jQuery validate plugin works internally in MVC

this is part of “understanding Mvc Unobtrusive Validation” series

  1. How the jQuery validate plugin works internally
  2. Understand the Html generated by the unobtrusive validation in MVC
  3. How the unobtrusive jQuery validate plugin works internally in MVC

What we will be talking about in this article

The parse method

we will explain the cycle of whats happening in the unobtrusive validation at the document load and will understand what is the role of every component

if we look at the end of the jquery.validate.unobtrusive.js we will find

$(function () {

so we called the parse() method and pass it document

so whats the parse method exactly

parse: function (selector) {
/// Parses all the HTML elements in the specified selector. It looks for input elements decorated
/// with the [data-val=true] attribute value and enables validation according to the data-val-*
/// attribute values.
/// </summary>
///Any valid jQuery selector.

$(selector).find(":input[data-val=true]").each(function () {
  $jQval.unobtrusive.parseElement(this, true);

var $forms = $(selector)

$forms.each(function () {
  var info = validationInfo(this);
  if (info) {

There is two sections in the parse method

1- parseElement section

parseElement(element, skipAttach)

$(selector).find(":input[data-val=true]").each(function () {
  $jQval.unobtrusive.parseElement(this, true);

so the first thing that happens we iterate over all the elements that have a data-val=true inside the selector that we passed (its document in our case)

then call parseElement() and pass it the element we want to validate and true for skipAttach

  • skipAttach parameter explanation

    a question could come up why we passed true to skipAttach not false

    skipAttach is a flag for calling validate() on the form

    If we passed false it will translate the rules on the element that we have passed then immediately call validate on the rules array and pass along other options used by the unobtrusive validation. (if there is still other element to be parsed they wont)

    We don’t want that. We first want to translate all the rules on every element in the form then after all the rules are translated we will call validate() which is basically the second part of the parse method

    So what other scenarios we would pass true to skipAttach ?

    If we want to add dynamic element to an already validated form we will pass true to skip validating the form again cause to wont make any effect (we will talk about dynamic validating element in the next article)

  • parseElement function explanation

    The parseElement() does mainly two things

    1. On the first call on an element in a form (No element was called before it in the same form)
      it will construct an object of options that will be passed to the validate() method, the options are used by the jquery.unobtrusive like a custom errorPlacement function, custom errorClass and an empty rules array (Note: what is responsible of doing all that is a private method called validationInfo(form) that is called within parseElement and when its called more than 1 time it will just return the same object so we can add, modify or call data/functions in it)
    2. For every element when we call parseElement it will understand the rules that are written on this element (the data-*) using the adapters (Will explain later how the adapters works)and then translate and add them to the rules array that was constructed in the first call
      (Note: every call to parseElement its result will be saved on the form itself using $.data(“unobtrusiveValidation“) that’s how the separate calls sync in the same data source)

2- validateInfo section

var $forms = $(selector)

$forms.each(function () {
  var info = validationInfo(this);
  if (info) {
  • validateInfo function explanation

    we already said calling validateInfo() will construct an object of options for the validate() method, the options are used by the jquery.unobtrusive like a custom errorPlacement function, custom errorClass and an empty rules array

    here we also called validationInfo() for every form in the page
    basically at this point calling validationInfo() we will only get the object stored on the form that was already populated in the first phase so we are using it as a getter

    then we are calling attachValidation() which is basically calling the validate() method passing it all the options populated by the validationInfo()

    data_validation = "unobtrusiveValidation";
    function validationInfo(form) {
      var $form = $(form),
           result = $,
           onResetProxy = $.proxy(onReset, form);
      if (!result) {
        result = {
          options: {  // options structure passed to jQuery Validate's validate() method
            errorClass: "input-validation-error",
            errorElement: "span",
            errorPlacement: $.proxy(onError, form),
            invalidHandler: $.proxy(onErrors, form),
            messages: {},
            rules: {},
            success: $.proxy(onSuccess, form)
          attachValidation: function () {
              .unbind("reset." + data_validation, onResetProxy)
              .bind("reset." + data_validation, onResetProxy)
          validate: function () {  // a validation function that is called by unobtrusive Ajax
            return $form.valid();
        $, result);
      return result;

    first we are checking if we already called this function on the form before by using $“unobtrusiveValidation“) if we did then do nothing and return the result

  • The return object explanation

    If its the first time we call validationInfo() then we construct a result object and will save it on the form using $.data() method this object will contain 3 parts:

    • An object which is the basically all the options that we will pass to the validate() method with an empty rules & messages arrays that will be constructed later
    • attachValidation() method will bind a custom event to the form itself “reset.unobtrusiveValidation” and after call validate on the method with all the options , this method will be called when the rules & messages arrays are completed¬† ( triggering the custom “reset” event will call onReset() method which will basically resets everything )
      function onReset(event) {  // 'this' is the form element
        var $form = $(this);
            .find(">*")  // If we were using valmsg-replace, get the underlying error

      so if we want to trigger the reset event to reset the form

    • a custom validate() method that will be called from unobtrusive ajax


I intentionally left the adapters section out when i talked about the parseElement() method because its complicated enough to be in a sub-section

We looked at how Html is generated using unobtrusive validation and how to add custom validation attribute in normal jquery.validate what links between the two is the Adapters

So What is the adapter’s responsibility ?

it is responsible for translating the Html data-* to a format that can be understood by the normal jquery.validate

If a user want to add a custom validation method using the unobtrusive validation he must also provide an adapter for it

the adapters collection resides in $.validator.unobtrusive.adapters

  • the adapters collection consist of all the default adapters defined by default in jquery.unobstrusive and the ones that the user has defined
  • It also contains 4 methods for adding custom adapters that we will take a look at later

so lets look at the most generic method which is

jQuery.validator.unobtrusive.adapters.add(adapterName, [params], fn)

you can consider this method the $.ajax method and the other three are helper methods that uses it

so lets explain the parameters

  • adapterName: is the adapter name as the name implies , and it matches the ruleName in the Html element (data-val-ruleName)
  • [params]: an optional parameter array that the validation method would use to complete validation
  • fn: Is called to map the Html data-*to rules and messages used by the validate() method and it has a parameter option passed to it which is an object containing the following properties:
    • element: the Html element being validated
    • form: the form element
    • message: the error message for this rule extracted from data-* attribute on the element
    • params: parameters that are used for the validation and its an array extracted from the data-* attributes on the Html element (data-val-ruleName-param1)
    • rules: The jquery rules array for this element , your expected to add to this array the rule(s) that this adapter is used for
      you will pass key/value pairs , the key is the validation rule name , the value is the parameters used for this rule (check this section in that article for adding custom rules to jquery.validate)
    • messages: The jquery messages array for this element, same as the rules object you are expected to fill it and its used as the messages object for this Html element in the validate method

    Note: There is no return result from the method. Whats happening manipulating the rules & messages arrays will directly be saved on the form itself using $.data(“unobtrusiveValidation“) you can check the parseElement method for the details of how the parameter where passed to the adapter function



<input id="val" type="text" name="val" data-val="true" data-val-between="Must be in the right range" data-val-between-min="5" data-val-between-max="30" />


//The adapter
  'between', ['min' ,'max'], function (options) {
    options.rules['between'] =  {
      min: options.params.min,
      max: options.params.max
    options.messages['between'] = options.message;

//The validation method
jQuery.validator.addMethod("between", function (value, element, params) {
  params.min == 5; //true
  params.max == 30; //true

so what about the other adapters

  • addBool
  • addSingleVal
  • addMinMax

They are all pretty simple if you understood the concept of adding a custom adapter using the add() method explained before

You can check Brad Wilson’s article he explained in it the adapters in depth

About these ads

19 thoughts on “How the unobtrusive jQuery validate plugin works internally in MVC

  1. I was very pleased to discover this great site. I need to to thank you for your
    time due to this fantastic read!! I definitely savored every bit of
    it and i also have you bookmarked to see new
    things in your website.

  2. Hi,
    Thank you very much , very helpful article.
    I want to know if it is possible to manually call parse method on Forms in order to validate a part of the form.

  3. Why $(fieldset).valid() always return valid.
    Is it because I don’t submit button and the form isn’t parsed yet.
    Thanks in advance..

    • I am not sure then, calling valid() of fieldset will try to validate all its children or not, if your calling $(fieldset).valid() and its returning true, and calling any input inside it and it returns false, then $(fieldset).valid() doesn’t work, you have to check each field inside it manually or better google it and see if there is a solution, Sorry It has been ages since I worked with it

    • If your using a typical mvc app, then I think no. This is by far the easiest, But my work for a while has been building SPA apps, jquery.validate is actually terrible in SPA apps, because its DOM dependent and SPA apps are all about dynamic contents

  4. As you said I have to call valid for every input element or every element that has data-val=true
    Thank you very much for your help

    • You are very welcome, I know this is not elegant. to validate All the inputs inside a form $(form).valid() will work, but I really don’t remember how to validate groups of inputs, anyway, hope it helps

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s