form-validation.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var FormValidation = function () {
  2. return {
  3. //main function to initiate the module
  4. init: function () {
  5. // for more info visit the official plugin documentation:
  6. // http://docs.jquery.com/Plugins/Validation
  7. var form1 = $('#form_sample_1');
  8. var error1 = $('.alert-error', form1);
  9. var success1 = $('.alert-success', form1);
  10. form1.validate({
  11. errorElement: 'span', //default input error message container
  12. errorClass: 'help-inline', // default input error message class
  13. focusInvalid: false, // do not focus the last invalid input
  14. ignore: "",
  15. rules: {
  16. name: {
  17. minlength: 2,
  18. required: true
  19. },
  20. email: {
  21. required: true,
  22. email: true
  23. },
  24. url: {
  25. required: true,
  26. url: true
  27. },
  28. number: {
  29. required: true,
  30. number: true
  31. },
  32. digits: {
  33. required: true,
  34. digits: true
  35. },
  36. creditcard: {
  37. required: true,
  38. creditcard: true
  39. },
  40. occupation: {
  41. minlength: 5,
  42. },
  43. category: {
  44. required: true
  45. }
  46. },
  47. invalidHandler: function (event, validator) { //display error alert on form submit
  48. success1.hide();
  49. error1.show();
  50. App.scrollTo(error1, -200);
  51. },
  52. highlight: function (element) { // hightlight error inputs
  53. $(element)
  54. .closest('.help-inline').removeClass('ok'); // display OK icon
  55. $(element)
  56. .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
  57. },
  58. unhighlight: function (element) { // revert the change dony by hightlight
  59. $(element)
  60. .closest('.control-group').removeClass('error'); // set error class to the control group
  61. },
  62. success: function (label) {
  63. label
  64. .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
  65. .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
  66. },
  67. submitHandler: function (form) {
  68. success1.show();
  69. error1.hide();
  70. }
  71. });
  72. //Sample 2
  73. $('#form_2_select2').select2({
  74. placeholder: "Select an Option",
  75. allowClear: true
  76. });
  77. var form2 = $('#form_sample_2');
  78. var error2 = $('.alert-error', form2);
  79. var success2 = $('.alert-success', form2);
  80. form2.validate({
  81. errorElement: 'span', //default input error message container
  82. errorClass: 'help-inline', // default input error message class
  83. focusInvalid: false, // do not focus the last invalid input
  84. ignore: "",
  85. rules: {
  86. name: {
  87. minlength: 2,
  88. required: true
  89. },
  90. email: {
  91. required: true,
  92. email: true
  93. },
  94. category: {
  95. required: true
  96. },
  97. options1: {
  98. required: true
  99. },
  100. options2: {
  101. required: true
  102. },
  103. occupation: {
  104. minlength: 5,
  105. },
  106. membership: {
  107. required: true
  108. },
  109. service: {
  110. required: true,
  111. minlength: 2
  112. }
  113. },
  114. messages: { // custom messages for radio buttons and checkboxes
  115. membership: {
  116. required: "Please select a Membership type"
  117. },
  118. service: {
  119. required: "Please select at least 2 types of Service",
  120. minlength: jQuery.format("Please select at least {0} types of Service")
  121. }
  122. },
  123. errorPlacement: function (error, element) { // render error placement for each input type
  124. if (element.attr("name") == "education") { // for chosen elements, need to insert the error after the chosen container
  125. error.insertAfter("#form_2_education_chzn");
  126. } else if (element.attr("name") == "membership") { // for uniform radio buttons, insert the after the given container
  127. error.addClass("no-left-padding").insertAfter("#form_2_membership_error");
  128. } else if (element.attr("name") == "service") { // for uniform checkboxes, insert the after the given container
  129. error.addClass("no-left-padding").insertAfter("#form_2_service_error");
  130. } else {
  131. error.insertAfter(element); // for other inputs, just perform default behavoir
  132. }
  133. },
  134. invalidHandler: function (event, validator) { //display error alert on form submit
  135. success2.hide();
  136. error2.show();
  137. App.scrollTo(error2, -200);
  138. },
  139. highlight: function (element) { // hightlight error inputs
  140. $(element)
  141. .closest('.help-inline').removeClass('ok'); // display OK icon
  142. $(element)
  143. .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
  144. },
  145. unhighlight: function (element) { // revert the change dony by hightlight
  146. $(element)
  147. .closest('.control-group').removeClass('error'); // set error class to the control group
  148. },
  149. success: function (label) {
  150. if (label.attr("for") == "service" || label.attr("for") == "membership") { // for checkboxes and radip buttons, no need to show OK icon
  151. label
  152. .closest('.control-group').removeClass('error').addClass('success');
  153. label.remove(); // remove error label here
  154. } else { // display success icon for other inputs
  155. label
  156. .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
  157. .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
  158. }
  159. },
  160. submitHandler: function (form) {
  161. success2.show();
  162. error2.hide();
  163. }
  164. });
  165. //apply validation on chosen dropdown value change, this only needed for chosen dropdown integration.
  166. $('.chosen, .chosen-with-diselect', form2).change(function () {
  167. form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
  168. });
  169. //apply validation on select2 dropdown value change, this only needed for chosen dropdown integration.
  170. $('.select2', form2).change(function () {
  171. form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
  172. });
  173. }
  174. };
  175. }();