form-validation.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. $(function () {
  2. $('#form_validation').validate({
  3. rules: {
  4. 'checkbox': {
  5. required: true
  6. },
  7. 'gender': {
  8. required: true
  9. }
  10. },
  11. highlight: function (input) {
  12. $(input).parents('.form-line').addClass('error');
  13. },
  14. unhighlight: function (input) {
  15. $(input).parents('.form-line').removeClass('error');
  16. },
  17. errorPlacement: function (error, element) {
  18. $(element).parents('.form-group').append(error);
  19. }
  20. });
  21. //Advanced Form Validation
  22. $('#form_advanced_validation').validate({
  23. rules: {
  24. 'date': {
  25. customdate: true
  26. },
  27. 'creditcard': {
  28. creditcard: true
  29. }
  30. },
  31. highlight: function (input) {
  32. $(input).parents('.form-line').addClass('error');
  33. },
  34. unhighlight: function (input) {
  35. $(input).parents('.form-line').removeClass('error');
  36. },
  37. errorPlacement: function (error, element) {
  38. $(element).parents('.form-group').append(error);
  39. }
  40. });
  41. //Custom Validations ===============================================================================
  42. //Date
  43. $.validator.addMethod('customdate', function (value, element) {
  44. return value.match(/^\d\d\d\d?-\d\d?-\d\d$/);
  45. },
  46. 'Please enter a date in the format YYYY-MM-DD.'
  47. );
  48. //Credit card
  49. $.validator.addMethod('creditcard', function (value, element) {
  50. return value.match(/^\d\d\d\d?-\d\d\d\d?-\d\d\d\d?-\d\d\d\d$/);
  51. },
  52. 'Please enter a credit card in the format XXXX-XXXX-XXXX-XXXX.'
  53. );
  54. //==================================================================================================
  55. });