2016-11-29 79 views
1

我在文本輸入上創建規則,但在創建規則時出現錯誤。錯誤說JQuery驗證在創建簡單規則時拋出錯誤

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"

這是從該行即將在驗證文件

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") 
    } 
}); 
+0

好的我解決了它,我需要添加一個角色=「form」屬性到窗體標籤! WTF! – user1186050

+0

好的我也注意到,如果我不首先驗證表單,就會拋出錯誤!它是否正確? – user1186050

+0

引用OP:*「我需要在表單標籤中添加一個'role =」form「'屬性!」*〜[我不這麼認爲。沒有它,工作得很好。](http://jsfiddle.net/1rmLksw8/) – Sparky

回答

0

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

必須第一致電表單元素上的.validate()方法插件初始化

$('#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?

你不 「驗證」 的形式第一次......你通過調用.validate()法 「初始化」它。