form-validation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var FormValidation = function () {
  2. return {
  3. //main function to initiate the module
  4. init: function (rules,messages) {
  5. $('#form_2_select2').select2({
  6. placeholder: "Select an Option",
  7. allowClear: true
  8. });
  9. var form2 = $('#form_sample_2');
  10. var error2 = $('.alert-error', form2);
  11. var success2 = $('.alert-success', form2);
  12. console.log(rules);
  13. form2.validate({
  14. errorElement: 'span', //default input error message container
  15. errorClass: 'help-inline', // default input error message class
  16. focusInvalid: false, // do not focus the last invalid input
  17. ignore: "",
  18. rules:rules,
  19. messages: messages,
  20. errorPlacement: function (error, element) { // render error placement for each input type
  21. if (element.attr("name") == "education") { // for chosen elements, need to insert the error after the chosen container
  22. error.insertAfter("#form_2_education_chzn");
  23. } else if (element.attr("name") == "membership") { // for uniform radio buttons, insert the after the given container
  24. error.addClass("no-left-padding").insertAfter("#form_2_membership_error");
  25. } else if (element.attr("name") == "service") { // for uniform checkboxes, insert the after the given container
  26. error.addClass("no-left-padding").insertAfter("#form_2_service_error");
  27. } else {
  28. error.insertAfter(element); // for other inputs, just perform default behavoir
  29. }
  30. },
  31. invalidHandler: function (event, validator) { //display error alert on form submit
  32. success2.hide();
  33. error2.show();
  34. App.scrollTo(error2, -200);
  35. },
  36. highlight: function (element) { // hightlight error inputs
  37. $(element)
  38. .closest('.help-inline').removeClass('ok'); // display OK icon
  39. $(element)
  40. .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
  41. },
  42. unhighlight: function (element) { // revert the change dony by hightlight
  43. $(element)
  44. .closest('.control-group').removeClass('error'); // set error class to the control group
  45. },
  46. success: function (label) {
  47. if (label.attr("for") == "service" || label.attr("for") == "membership") { // for checkboxes and radip buttons, no need to show OK icon
  48. label
  49. .closest('.control-group').removeClass('error').addClass('success');
  50. label.remove(); // remove error label here
  51. } else { // display success icon for other inputs
  52. label
  53. .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
  54. .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
  55. }
  56. },
  57. submitHandler: function (form) {
  58. success2.show();
  59. error2.hide();
  60. form.submit();
  61. }
  62. });
  63. //apply validation on chosen dropdown value change, this only needed for chosen dropdown integration.
  64. $('.chosen, .chosen-with-diselect', form2).change(function () {
  65. form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
  66. });
  67. //apply validation on select2 dropdown value change, this only needed for chosen dropdown integration.
  68. $('.select2', form2).change(function () {
  69. form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
  70. });
  71. }
  72. };
  73. }();
  74. //配置通用的默认提示语
  75. $.extend($.validator.messages, {
  76. required: "这是必填字段",
  77. remote: "请修正此字段",
  78. email: "请输入有效的电子邮件地址",
  79. url: "请输入有效的网址",
  80. date: "请输入有效的日期",
  81. dateISO: "请输入有效的日期 (YYYY-MM-DD)",
  82. number: "请输入有效的数字",
  83. digits: "只能输入数字",
  84. creditcard: "请输入有效的信用卡号码",
  85. equalTo: "你的输入不相同",
  86. extension: "请输入有效的后缀",
  87. maxlength: $.validator.format("最多可以输入 {0} 个字符"),
  88. minlength: $.validator.format("最少要输入 {0} 个字符"),
  89. rangelength: $.validator.format("请输入长度在 {0} 到 {1} 之间的字符串"),
  90. range: $.validator.format("请输入范围在 {0} 到 {1} 之间的数值"),
  91. max: $.validator.format("请输入不大于 {0} 的数值"),
  92. min: $.validator.format("请输入不小于 {0} 的数值"),
  93. id_card:"请输入正确的身份证号码",
  94. });