我查阅这篇文章 https://stackoverflow.com/a/3624622/728750 https://stackoverflow.com/a/27402711/728750
<form action="#">
<input type="text" id="name1" class="fileName" name="name1" />
<input type="text" id="name2" class="fileName" name="name2"/>
<input type="text" id="name3" class="fileName" name="name3"/>
<input type="text" id="name4" class="fileName" name="name4"/>
<input type="submit" />
</form>
$(document).ready(function(){
$.validator.addClassRules({
fileName:{
required: true
}
});
$.validator.messages.fileName = 'File name mandatory!';
$("form").validate();
});
my js fiddle http://jsfiddle.net/tridip/Nbcj9/395/
答案 0 :(得分:2)
将必需更改为 cRequired ,然后通过添加$ .validator.addMethod(“cRequired”,$。validator)覆盖 cRequired 消息。 methods.required,“文件名必填!”);
$(document).ready(function(){
$.validator.addClassRules({
fileName:{
cRequired: true
}
});
$.validator.addMethod("cRequired", $.validator.methods.required,"File name mandatory!");
$("form").validate();
});
更新了小提琴 - http://jsfiddle.net/Nbcj9/397/
答案 1 :(得分:1)
由于所需规则有默认消息,您可以更改它:
$.extend($.validator.messages, {required: "File name mandatory!"});
$.validator.addClassRules("fileName", {required: true});
$(function () {
$("form").validate();
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
<form action="#">
<input type="text" id="name1" class="fileName" name="name1" />
<input type="text" id="name2" class="fileName" name="name2"/>
<input type="text" id="name3" class="fileName" name="name3"/>
<input type="text" id="name4" class="fileName" name="name4"/>
<input type="submit" />
</form>