Robo Extract Template

validate.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * PHP Email Form Validation - v2.3
  3. * URL: https://bootstrapmade.com/php-email-form/
  4. * Author: BootstrapMade.com
  5. */
  6. !(function($) {
  7. "use strict";
  8. $('form.php-email-form').submit(function(e) {
  9. e.preventDefault();
  10. var f = $(this).find('.form-group'),
  11. ferror = false,
  12. emailExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
  13. f.children('input').each(function() { // run all inputs
  14. var i = $(this); // current input
  15. var rule = i.attr('data-rule');
  16. if (rule !== undefined) {
  17. var ierror = false; // error flag for current input
  18. var pos = rule.indexOf(':', 0);
  19. if (pos >= 0) {
  20. var exp = rule.substr(pos + 1, rule.length);
  21. rule = rule.substr(0, pos);
  22. } else {
  23. rule = rule.substr(pos + 1, rule.length);
  24. }
  25. switch (rule) {
  26. case 'required':
  27. if (i.val() === '') {
  28. ferror = ierror = true;
  29. }
  30. break;
  31. case 'minlen':
  32. if (i.val().length < parseInt(exp)) {
  33. ferror = ierror = true;
  34. }
  35. break;
  36. case 'email':
  37. if (!emailExp.test(i.val())) {
  38. ferror = ierror = true;
  39. }
  40. break;
  41. case 'checked':
  42. if (! i.is(':checked')) {
  43. ferror = ierror = true;
  44. }
  45. break;
  46. case 'regexp':
  47. exp = new RegExp(exp);
  48. if (!exp.test(i.val())) {
  49. ferror = ierror = true;
  50. }
  51. break;
  52. }
  53. i.next('.validate').html((ierror ? (i.attr('data-msg') !== undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
  54. }
  55. });
  56. f.children('textarea').each(function() { // run all inputs
  57. var i = $(this); // current input
  58. var rule = i.attr('data-rule');
  59. if (rule !== undefined) {
  60. var ierror = false; // error flag for current input
  61. var pos = rule.indexOf(':', 0);
  62. if (pos >= 0) {
  63. var exp = rule.substr(pos + 1, rule.length);
  64. rule = rule.substr(0, pos);
  65. } else {
  66. rule = rule.substr(pos + 1, rule.length);
  67. }
  68. switch (rule) {
  69. case 'required':
  70. if (i.val() === '') {
  71. ferror = ierror = true;
  72. }
  73. break;
  74. case 'minlen':
  75. if (i.val().length < parseInt(exp)) {
  76. ferror = ierror = true;
  77. }
  78. break;
  79. }
  80. i.next('.validate').html((ierror ? (i.attr('data-msg') != undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
  81. }
  82. });
  83. if (ferror) return false;
  84. var this_form = $(this);
  85. var action = $(this).attr('action');
  86. if( ! action ) {
  87. this_form.find('.loading').slideUp();
  88. this_form.find('.error-message').slideDown().html('The form action property is not set!');
  89. return false;
  90. }
  91. this_form.find('.sent-message').slideUp();
  92. this_form.find('.error-message').slideUp();
  93. this_form.find('.loading').slideDown();
  94. if ( $(this).data('recaptcha-site-key') ) {
  95. var recaptcha_site_key = $(this).data('recaptcha-site-key');
  96. grecaptcha.ready(function() {
  97. grecaptcha.execute(recaptcha_site_key, {action: 'php_email_form_submit'}).then(function(token) {
  98. php_email_form_submit(this_form,action,this_form.serialize() + '&recaptcha-response=' + token);
  99. });
  100. });
  101. } else {
  102. php_email_form_submit(this_form,action,this_form.serialize());
  103. }
  104. return true;
  105. });
  106. function php_email_form_submit(this_form, action, data) {
  107. $.ajax({
  108. type: "POST",
  109. url: action,
  110. data: data,
  111. timeout: 40000
  112. }).done( function(msg){
  113. if (msg.trim() == 'OK') {
  114. this_form.find('.loading').slideUp();
  115. this_form.find('.sent-message').slideDown();
  116. this_form.find("input:not(input[type=submit]), textarea").val('');
  117. } else {
  118. this_form.find('.loading').slideUp();
  119. if(!msg) {
  120. msg = 'Form submission failed and no error message returned from: ' + action + '<br>';
  121. }
  122. this_form.find('.error-message').slideDown().html(msg);
  123. }
  124. }).fail( function(data){
  125. console.log(data);
  126. var error_msg = "Form submission failed!<br>";
  127. if(data.statusText || data.status) {
  128. error_msg += 'Status:';
  129. if(data.statusText) {
  130. error_msg += ' ' + data.statusText;
  131. }
  132. if(data.status) {
  133. error_msg += ' ' + data.status;
  134. }
  135. error_msg += '<br>';
  136. }
  137. if(data.responseText) {
  138. error_msg += data.responseText;
  139. }
  140. this_form.find('.loading').slideUp();
  141. this_form.find('.error-message').slideDown().html(error_msg);
  142. });
  143. }
  144. })(jQuery);