×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Arung Isyadi
Added: Apr 13, 2014 5:20 AM
Modified: Apr 13, 2014 5:22 AM
Views: 1809
  1. jQuery(document).ready(function ($) {
  2.     $('#subscribe-form').submit(function () {
  3.  
  4.         $('#subscribe-form .subscribe-error').remove();
  5.         var hasError = false;
  6.  
  7.         $('#subscribe-form .requiredField').each(function () {
  8.             if (jQuery.trim($(this).attr('name')) == 'f-sub-terms' && $(this).prop('checked') != true) {
  9.                 $(this).parent('label').append('<span class="subscribe-error">You must accept the terms</span>');
  10.                 $(this).addClass('inputError');
  11.                 hasError = true;
  12.             } else if (jQuery.trim($(this).val()) == '') {
  13.                 $(this).prev('label').append('<span class="subscribe-error">Required</span>');
  14.                 $(this).addClass('inputError');
  15.                 hasError = true;
  16.             } else {
  17.                 if ($(this).hasClass('email')) {
  18.                     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  19.                     if (!emailReg.test(jQuery.trim($(this).val()))) {
  20.                         $(this).prev('label').append('<span class="subscribe-error">Invalid</span>');
  21.                         $(this).addClass('inputError');
  22.                         hasError = true;
  23.                     }
  24.                 }
  25.             }
  26.         });
  27.  
  28.         if (!hasError) {
  29.             jQuery.post(
  30.                 'https://www.windermere.org.au/wp-admin/admin-ajax.php', {
  31.                     'action': 'ajax_subscribe',
  32.                     'First Name': $("#f-sub-firstname").val(),
  33.                     'Last Name': $("#f-sub-lastname").val(),
  34.                     'Email': $("#f-sub-email").val(),
  35.                     'Interests': $('input[name="f-sub-interests[]"]:checked').map(function () {
  36.                         return this.value;
  37.                     }).get().join(', ')
  38.                 }
  39.             )
  40.                 .done(function () {
  41.                     $('#subscribe-form').after('<div id="subscribe-confirmation">You are subscribed to our list</div>').fadeOut('fast', function () {
  42.                         $('#subscribe-confirmation').fadeIn('fast').delay(7000).fadeOut();
  43.                     });
  44.                 })
  45.                 .fail(function () {
  46.                     alert("There has been an error submitting your details. Please try again.");
  47.                 });
  48.         }
  49.         return false;
  50.  
  51.     });
  52. });