你可以簡單地把你的ErrMsgs.js
文件中的以下內容:
$.extend(jQuery.validator.messages, {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: $.validator.format("Please enter no more than {0} characters."),
minlength: $.validator.format("Please enter at least {0} characters."),
rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
range: $.validator.format("Please enter a value between {0} and {1}."),
max: $.validator.format("Please enter a value less than or equal to {0}."),
min: $.validator.format("Please enter a value greater than or equal to {0}.")
});
然後包括它像任何其他JavaScript文件:
<script type="text/javascript" src="ErrMsgs.js"></script>
編輯:
基於這些變數的評論需要在另一個地方使用,也許下面的工作。
var msg1 = "This field is required.",
msg2 = "Please fix this field.",
// etc., etc.
$.extend(jQuery.validator.messages, {
required: msg1,
remote: msg2,
// etc. etc.
});
$.extend(jQuery.xyz.messages, {
required: msg1,
remote: msg2,
// etc. etc.
});
這將幫助你 http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file –
@KanishkaPanamaldeniya感謝快速回復。 Tats有用,但我需要在「jquery.validate.min.js」以外的其他文件中聲明變量,並且需要在「jquery.validate.min.js」文件中使用這些變量。 – sandeep