创建简单规则时,JQuery验证抛出错误

时间:2016-11-29 01:22:34

标签: jquery jquery-validate

我正在为文本输入创建规则,但在创建规则时出错。错误说

  

https://localhost:44368/Scripts/jquery.validate.js第14行第14行未处理的异常   0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性“设置”“

来自验证文件中的这一行

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

其中

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

未定义。这是为什么???

这是元素

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

这是规则

$('#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")
  }
});

1 个答案:

答案 0 :(得分:0)

  

JavaScript运行时错误:无法获取未定义或空引用的属性“设置”

必须首先初始化插件,方法是调用表单元素上的.validate()方法。

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

好的我也注意到如果我不先验证表单就会抛出错误!这是对的吗?

您不首先“验证”表单...通过调用.validate()方法“初始化”