Letlabo Nature Reserve
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

contactform.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. jQuery(document).ready(function($) {
  2. "use strict";
  3. //Contact
  4. $('form.contactForm').submit(function() {
  5. var f = $(this).find('.form-group'),
  6. ferror = false,
  7. emailExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
  8. f.children('input').each(function() { // run all inputs
  9. var i = $(this); // current input
  10. var rule = i.attr('data-rule');
  11. if (rule !== undefined) {
  12. var ierror = false; // error flag for current input
  13. var pos = rule.indexOf(':', 0);
  14. if (pos >= 0) {
  15. var exp = rule.substr(pos + 1, rule.length);
  16. rule = rule.substr(0, pos);
  17. } else {
  18. rule = rule.substr(pos + 1, rule.length);
  19. }
  20. switch (rule) {
  21. case 'required':
  22. if (i.val() === '') {
  23. ferror = ierror = true;
  24. }
  25. break;
  26. case 'minlen':
  27. if (i.val().length < parseInt(exp)) {
  28. ferror = ierror = true;
  29. }
  30. break;
  31. case 'email':
  32. if (!emailExp.test(i.val())) {
  33. ferror = ierror = true;
  34. }
  35. break;
  36. case 'checked':
  37. if (! i.is(':checked')) {
  38. ferror = ierror = true;
  39. }
  40. break;
  41. case 'regexp':
  42. exp = new RegExp(exp);
  43. if (!exp.test(i.val())) {
  44. ferror = ierror = true;
  45. }
  46. break;
  47. }
  48. i.next('.validation').html((ierror ? (i.attr('data-msg') !== undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
  49. }
  50. });
  51. f.children('textarea').each(function() { // run all inputs
  52. var i = $(this); // current input
  53. var rule = i.attr('data-rule');
  54. if (rule !== undefined) {
  55. var ierror = false; // error flag for current input
  56. var pos = rule.indexOf(':', 0);
  57. if (pos >= 0) {
  58. var exp = rule.substr(pos + 1, rule.length);
  59. rule = rule.substr(0, pos);
  60. } else {
  61. rule = rule.substr(pos + 1, rule.length);
  62. }
  63. switch (rule) {
  64. case 'required':
  65. if (i.val() === '') {
  66. ferror = ierror = true;
  67. }
  68. break;
  69. case 'minlen':
  70. if (i.val().length < parseInt(exp)) {
  71. ferror = ierror = true;
  72. }
  73. break;
  74. }
  75. i.next('.validation').html((ierror ? (i.attr('data-msg') != undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
  76. }
  77. });
  78. if (ferror) return false;
  79. else var str = $(this).serialize();
  80. var action = $(this).attr('action');
  81. if( ! action ) {
  82. action = 'contactform/contactform.php';
  83. }
  84. $.ajax({
  85. type: "POST",
  86. url: action,
  87. data: str,
  88. success: function(msg) {
  89. // alert(msg);
  90. if (msg == 'OK') {
  91. $("#sendmessage").addClass("show");
  92. $("#errormessage").removeClass("show");
  93. $('.contactForm').find("input, textarea").val("");
  94. } else {
  95. $("#sendmessage").removeClass("show");
  96. $("#errormessage").addClass("show");
  97. $('#errormessage').html(msg);
  98. }
  99. }
  100. });
  101. return false;
  102. });
  103. });