title: jQuery Validation: $.data($('form')[0], 'validator').settings returns undefined
body: <p>I have an ASP.Net MVC Project and I am using unobtrusive jQuery Validation.
  to add validation when an element loses focus, I am calling</p>
  <pre><code>$(document).ready(function () {
      // enable validation when an input loses focus.
      var settngs = $.data($('form')[0], 'validator').settings;
      settngs.onfocusout = function (element) { $(element).valid(); };
  });
  </code></pre>
  <p>This is working on one project while it throws this exception on another project because $.data($('form')[0], 'validator') returns undefined ($.data($('form')[0]) returns an empty object):</p>
  <blockquote>
  <p>Uncaught TypeError: Cannot read property 'settings' of undefined</p>
  </blockquote>
  <p>However, jQuery Validation is working fine when the submit button is pressed, so everything else should be set up correctly.</p>
  <p>I am loading these scripts at the end of the body tag: (the function listed above is in customvalidations.js so it should execute after the validator is applied to the form)</p>
  <pre><code>&lt;script src=&quot;/Staffing/Scripts/jquery-2.1.1.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/globalize/globalize.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/globalize/cultures/globalize.culture.de-DE.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/bootstrap.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/respond.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/bootstrap-datepicker.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/bootstrap-datepicker-globalize.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/locales/bootstrap-datepicker.de.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/jquery.unobtrusive-ajax.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/jquery.validate.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/jquery.validate.globalize.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/jquery.validate.unobtrusive.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/localization/messages_de.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/customvalidations.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/uiadditions.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;/Staffing/Scripts/default.js&quot;&gt;&lt;/script&gt;
  </code></pre>
  <p><strong>Solution:</strong>
  This is the code that works:</p>
  <pre><code>$(document).ready(function () {
      // enable validation when an input loses focus.
      var allForms = $('form');
      $.each(allForms, function (key, value) {
          var val = $.data(value, 'validator');
          if (val != undefined) {
              var settngs = val.settings;
              settngs.onfocusout = function (element) { $(element).valid(); };
          }});
  });
  </code></pre>
  <p>The problem was that the new Validation plugin checks if there are any elements present which should be validated and I had 2 forms on the same page with the first form not having any validated input elements.</p>