JQuery validate throws error when creating a simple rule

1

I'm creating a rule on a text input, but getting an error when the rule gets created. The error says

Unhandled exception at line 147, column 4 in https://localhost:44368/Scripts/jquery.validate.js 0x800a138f - JavaScript runtime error: Unable to get property 'settings' of undefined or null reference"

which is coming from this line in the validate file

settings = $.data( element.form, "validator" ).settings;

where

$.data( element.form, "validator" )

is undefined. Why is this???

Here is the element

<input class="form-control input-lg" id="zipCodeText" type="text" placeholder="e. g. 94901" value="94102">

Here is the rule

$('#zipCodeText').rules("add", {
  required: true,
  minlength: 5,
  maxlength: 5,
  messages: {
    required: "Required input",
    minlength: jQuery.validator.format("Please, {0} characters are necessary"),
    maxlength: jQuery.validator.format("Please, {0} characters are necessary")
  }
});
jquery
jquery-validate
asked on Stack Overflow Nov 29, 2016 by user1186050 • edited Nov 29, 2016 by Sparky

1 Answer

0

JavaScript runtime error: Unable to get property 'settings' of undefined or null reference"

You must first initialize the plugin by calling the .validate() method on your form element.

$('#yourform').validate({ // <-- INITIALIZE the plugin on your form
    // any options, rules, or callbacks
});

ok I also noticed that the error is thrown if I don't validate the form first! is this correct?

You don't "validate" the form first... you "initialize" it by calling the .validate() method.

answered on Stack Overflow Nov 29, 2016 by Sparky • edited Nov 29, 2016 by Sparky

User contributions licensed under CC BY-SA 3.0