覆盖jquery验证消息

时间:2014-01-23 01:40:11

标签: javascript jquery jquery-validate

我正在尝试覆盖jquery验证消息中的默认消息,因为文档告诉我没有使用它仍然显示“此字段是必需的”。 ?

http://jsfiddle.net/7Yrz7/

$(function () {    
    $('form').validate({
        rules: {
            email:"required",
            password:"required",
            messages: {
                email: "Please enter an email address.",
                password: "This field is required."
            }
        }
    });
});

2 个答案:

答案 0 :(得分:3)

应该是:

$("form").validate({
    rules: {
        email: "required",
        password: "required"
    }, // <-- here
    messages: {
        email: "Please enter an email address.",
        password: "This field is required."
   }
});

在此处使用rules之前,您需要关闭messages

<强> Updated Fiddle

答案 1 :(得分:0)

要使用Validate插件,您必须先添加Rule然后再添加消息。

脚本

$(document).ready(function () {

$('#myform').validate({ // initialize the plugin
    rules: {
            field1: {
                      required: true,
                      email: true
                    },
            field2: {
                      required: true,
                      minlength: 5
                    }
            }, // end of rules
   messages: {
                field1: "You cannot leave field1 blank",
                field2: "You cannot leave field1 blank"
              }// end of message


    });

});

HTML

<form id="myform">
  <input type="text" name="field1" />
  <input type="text" name="field2" />
  <input type="submit" />
</form>

选项:http://jqueryvalidation.org/validate

方法:http://jqueryvalidation.org/category/plugin/

标准规则:http://jqueryvalidation.org/category/methods/

additional-methods.js文件提供的可选规则:

maxWords
minWords
rangeWords
letterswithbasicpunc
alphanumeric
lettersonly
nowhitespace
ziprange
zipcodeUS
integer
vinUS
dateITA
dateNL
time
time12h
phoneUS
phoneUK
mobileUK
phonesUK
postcodeUK
strippedminlength
email2 (optional TLD)
url2 (optional TLD)
creditcardtypes
ipv4
ipv6
pattern
require_from_group
skip_or_fill_minimum
accept
extension