Adding methods to $.validate
I'm using a framework that provides matching client and server-side validation for forms which leverages jQuery's Validate. On page load, the browser makes 2 ajax calls. The first retrieves some JSON which describe the validation rules. The second returns HTML which merely contains a <script> tag and some JS with the intention of adding methods to $.validator. Unfortunately, these methods are not being added. Below is a snippet of what's being returned...
- <script type="text/javascript"> /*<![CDATA[*/ jQuery(function ($) { $.validator.addMethod("nohtml", function (v, e, o) { var m = v.search("</?\\w+((\\s+\\w+(\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+))?)+\\s*|\\s*)/?>"); return m === -1; }, $.format("value cannot contain any HTML tags."));
- // A bunch more calls to $.validator.addMethod() for additional methods
- }); /*]]>*/ </script>
After this ajax call completes, I begin to fill out an html form. When I get to a field using "nohtml", my browser console is throwing an exception indicating the method "nohtml" does not exist in $.validator.
If I copy/paste the value of the outer-most closure into the console, I'm able to see that nohtml() has been added to $.validator.
Could anybody explain to me why using a closure is not updating the instance of $.validator?