title: Validating Credit Card Inputs with jQuery
body: <p>I'm working on form validation in jQuery.  The form is working fine when just trying to validate the name, email and a check to ensure one activity is selected.  If a user selects credit card for payment I must run a validation on the cc number, zip code and cvv.  I tried to refactor the code and I think my logic is off somewhere as when I submit the form with no data, the only error message is on the CC digits and the form submits, when it should be prevented (via preventDefault).  No errors in console, so I know my logic must be messed up somewhere.  Any takers?</p>
  
  <pre><code>var intRegex = /[0-9 -()+]+$/;
  
  
  // Name can't be blank
  function validateName(){
      var name = $('#name').val();
      var error_name = true
      if (name === ''){
          $(".error_name").show();
          console.log('name cant be empty');
      } else {
          error_name = false;
      }
      return error_name
  
  }
  // Email must be valid
  function validateEmail(){
      var email = $('#mail').val();
      var error_mail = true;
      var pattern = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
      error_email = pattern.test(email);
      if (!error_email){
          $(".error_email").show();
          error_email = true;
      } else {
          error_email = false;
      }
      return error_email
  }
  // One Activity Must Be Selected
  function validateActivities(){
      var totalActivities = $('.activities input[type="checkbox"]:checked').length;
      var error_activity = true;
      if (totalActivities &lt; 1) {
          $(".error_activities").show();
      } else {
          error_activity = false;
      }
      return error_activity
  }
  
  // Validate the Credit Card Number --Make sure is between 13 to 16 digit number
  function validateCC(){
      var cc = $("#cc-num").val();
      var errorCC = true;
      errorCC = intRegex.test(cc);
      if ((!errorCC) || ((cc.length &lt; 13) || (cc.length &gt; 16))){
          $(".error_cc").show();
          errorCC = true;
      } else {
          errorCC = false;
      }
      return errorCC
  }
  
  function validateZip(){
      var zip = $("#zip").val();
      var errorZip = true;
      errorZip = intRegex.text(zip);
      if ((!errorZip)||(zip!= 5)){
          $(".error_zip").show();
          errorZip = true;
      } else {
          errorZip = false;
      }
      return errorZip
  }
  
  function validateCVV(){
      var cvv = $("#cvv").val().length;
      var errorCVV = true;
      errorCVV = intRegex.text(cvv);
      if ((!errorCVV)||(cvv!= 3)){
          $(".error_cvv").show();
          errorCVV = true;
      } else {
          errorCVV = false;
      }
      return errorCVV
  }
  
  //Check for Credit Card Issue -- Any problems with CC, Zip Code or CVV
  function validateCredit(){
      var credit = $("#payment option[value='credit card']")
      errorCC = validateCC();
      errorZip = validateZip();
      errorCVV = validateCVV();
      isCreditIssue = false;
      if (credit.prop('selected', true)){
          if ((errorCC) || (errorZip) || (errorCVV)){
          isCreditIssue = true;
          console.log('credit issue');
          }
       }
      return isCreditIssue
  }
  
  
  // On Form Submission Validate Form
  $("#contact_submit button").click(function(event){
      error_name = validateName();
      error_email = validateEmail();
      error_activity = validateActivities();
      isCreditIssue = validateCredit();
  
      if ((error_name) || (error_email) || (error_activity) || (isCreditIssue)){
          event.preventDefault();
      }
  
  
  
  });
  </code></pre>