antondaniel787

Untitled

Mar 10th, 2014
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. $(document).ready(function() {
  2. $("#feedbackSubmit").click(function() {
  3. //clear any errors
  4. contactForm.clearErrors();
  5.  
  6. //do a little client-side validation -- check that each field has a value and e-mail field is in proper format
  7. var hasErrors = false;
  8. $('#feedbackForm input,textarea').each(function() {
  9. if (!$(this).val()) {
  10. hasErrors = true;
  11. contactForm.addError($(this));
  12. }
  13. });
  14. var $email = $('#email');
  15. if (!contactForm.isValidEmail($email.val())) {
  16. hasErrors = true;
  17. contactForm.addError($email);
  18. }
  19.  
  20. //if there are any errors return without sending e-mail
  21. if (hasErrors) {
  22. return false;
  23. }
  24.  
  25. //send the feedback e-mail
  26. $.ajax({
  27. type: "POST",
  28. url: "sendmail.php",
  29. data: $("#feedbackForm").serialize(),
  30. success: function(data)
  31. {
  32. contactForm.addAjaxMessage(data.message, false);
  33. //get new Captcha on success
  34. $('#captcha').attr('src', 'securimage/securimage_show.php?' + Math.random());
  35. },
  36. error: function(response)
  37. {
  38. contactForm.addAjaxMessage(response.responseJSON.message, true);
  39. }
  40. });
  41. return false;
  42. });
  43. });
  44.  
  45. //namespace as not to pollute global namespace
  46. var contactForm = {
  47. isValidEmail: function (email) {
  48. var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  49. return regex.test(email);
  50. },
  51. clearErrors: function () {
  52. $('#emailAlert').remove();
  53. $('#feedbackForm .help-block').hide();
  54. $('#feedbackForm .form-group').removeClass('has-error');
  55. },
  56. addError: function ($input) {
  57. $input.siblings('.help-block').show();
  58. $input.parent('.form-group').addClass('has-error');
  59. },
  60. addAjaxMessage: function(msg, isError) {
  61. $("#feedbackSubmit").after('<div id="emailAlert" class="alert alert-' + (isError ? 'danger' : 'success') + '" style="margin-top: 5px;">' + $('<div/>').text(msg).html() + '</div>');
  62. }
  63. };
Advertisement
Add Comment
Please, Sign In to add comment