jQuery.Validate.Extend.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function ($) {
  2. $.validator.addMethod('dropdownlistrequired', function (value, element, params) {
  3. return (value != "" && value != "-1");
  4. });
  5. $.validator.unobtrusive.adapters.add("dropdownlistrequired", function (options) {
  6. options.rules["dropdownlistrequired"] = {
  7. };
  8. options.messages["dropdownlistrequired"] = options.message;
  9. });
  10. $.validator.addMethod('daterequired', function (value, element, params) {
  11. return (value != "" && value != "0001-01-01");
  12. });
  13. $.validator.unobtrusive.adapters.add("daterequired", function (options) {
  14. options.rules["daterequired"] = {
  15. };
  16. options.messages["daterequired"] = options.message;
  17. });
  18. $.validator.addMethod('totallength', function (value, element, params) {
  19. var totalString = value;
  20. $.each(eval('('+params.otherControls+')'), function (i, v) {
  21. totalString += $("#" + v).val();
  22. });
  23. if (totalString.length == params.length) {
  24. return true;
  25. } else {
  26. $.each(eval('(' + params.otherControls + ')'), function (i, v) {
  27. totalString += $("#" + v).validate();
  28. });
  29. }
  30. });
  31. $.validator.unobtrusive.adapters.add("totallength", ["length", "other"], function (options) {
  32. options.rules["totallength"] = {
  33. length: options.params.length,
  34. otherControls: options.params.other
  35. };
  36. if (options.message) {
  37. options.messages['totallength'] = options.message;
  38. }
  39. });
  40. $.extend($.fn.validatebox.defaults.rules, {
  41. daterequired: {
  42. validator: function (value, param) {
  43. return (value != "" && value != "0001-01-01");
  44. },
  45. message: 'Please enter at least {0} characters.'
  46. }
  47. });
  48. $.validator.methods.date = function (value, element) {
  49. // Date.parse函数源自另外一个处理日期的库
  50. return this.optional(element) || Date.parse(value) != null;
  51. };
  52. jQuery.extend(jQuery.validator.messages, {
  53. required: "必须填写",
  54. remote: "请修正该字段",
  55. email: "请输入正确格式的电子邮件",
  56. url: "请输入合法的网址",
  57. date: "请输入合法的日期",
  58. dateISO: "请输入合法的日期 (ISO).",
  59. number: "请输入合法的数字",
  60. digits: "只能输入整数",
  61. creditcard: "请输入合法的信用卡号",
  62. equalTo: "请再次输入相同的值",
  63. accept: "请输入拥有合法后缀名的字符串",
  64. maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
  65. minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
  66. rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
  67. range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
  68. max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
  69. min: jQuery.validator.format("请输入一个最小为 {0} 的值")
  70. });
  71. } (jQuery));