contact_form.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. $(document).ready(function(){
  2. // Contact Form Submition
  3. function checkRequire(formId , targetResp){
  4. targetResp.html('');
  5. var email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
  6. var url = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/;
  7. var image = /\.(jpe?g|gif|png|PNG|JPE?G)$/;
  8. var mobile = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/;
  9. var facebook = /^(https?:\/\/)?(www\.)?facebook.com\/[a-zA-Z0-9(\.\?)?]/;
  10. var twitter = /^(https?:\/\/)?(www\.)?twitter.com\/[a-zA-Z0-9(\.\?)?]/;
  11. var google_plus = /^(https?:\/\/)?(www\.)?plus.google.com\/[a-zA-Z0-9(\.\?)?]/;
  12. var check = 0;
  13. $('#er_msg').remove();
  14. var target = (typeof formId == 'object')? $(formId):$('#'+formId);
  15. target.find('input , textarea , select').each(function(){
  16. if($(this).hasClass('require')){
  17. if($(this).val().trim() == ''){
  18. check = 1;
  19. $(this).focus();
  20. targetResp.html('You missed out some fields.');
  21. $(this).addClass('error');
  22. return false;
  23. }else{
  24. $(this).removeClass('error');
  25. }
  26. }
  27. if($(this).val().trim() != ''){
  28. var valid = $(this).attr('data-valid');
  29. if(typeof valid != 'undefined'){
  30. if(!eval(valid).test($(this).val().trim())){
  31. $(this).addClass('error');
  32. $(this).focus();
  33. check = 1;
  34. targetResp.html($(this).attr('data-error'));
  35. return false;
  36. }else{
  37. $(this).removeClass('error');
  38. }
  39. }
  40. }
  41. });
  42. return check;
  43. }
  44. $(".submitForm").on("click", function() {
  45. var _this = $(this);
  46. var targetForm = _this.closest('form');
  47. var errroTarget = targetForm.find('.response');
  48. var check = checkRequire(targetForm , errroTarget);
  49. if(check == 0){
  50. var formDetail = new FormData(targetForm[0]);
  51. $.ajax({
  52. method : 'post',
  53. url : 'contactmail.php',
  54. data:formDetail,
  55. cache:false,
  56. contentType: false,
  57. processData: false
  58. }).done(function(resp){
  59. if(resp == 1){
  60. targetForm.find('input').val('');
  61. targetForm.find('textarea').val('');
  62. errroTarget.html('<p style="color:green;">Mail has been sent successfully.</p>');
  63. }else{
  64. errroTarget.html('<p style="color:red;">Something went wrong please try again later.</p>');
  65. }
  66. });
  67. }
  68. });
  69. });