title: Javascript using Validatious script to toggle required fields based on if Checkbox is checked
body: <p>I have a checkbox called "purchaseorder". If checked, I'd like to make all of the CC fields not required. However, my script doesn't appear to be working. Any ideas?</p>
  
  <p>The checkbox's name is purchaseorder. The other fields are CC field names. By default, CC fields are required. Only if a customer checks "purchaseorder" should they not be required.</p>
  
  <p>Validatious: <a href="http://validatious.org/" rel="nofollow">http://validatious.org/</a></p>
  
  <pre><code>function ToggleCreditcardpayment() {
    if ($('input[type=checkbox][name=purchaseorder]').is(':checked')) {
      $('#NameOnCard').removeClass('required');
      $('select[name=CreditCardType]').removeClass('required');
      $('select[name=CardExpMonth]').removeClass('required');
      $('select[name=CardExpYear]').removeClass('required');
      $('#CardNumber').removeClass('required');
      $('#CVC').removeClass('required');
  
      $('#NameOnCard').attr('disabled', true);
      $('select[name=CreditCardType]').attr('disabled', true);
      $('select[name=CardExpMonth]').attr('disabled', true);
      $('select[name=CardExpYear]').attr('disabled', true);
      $('#CardNumber').attr('disabled', true);
      $('#CVC').attr('disabled', true);
  
      $('#customernumber').addClass('required');
      $('#customernumber').attr('disabled', false);
      $('#payment-method-account .error').removeClass('error');
      $('#payment-method-account ul').remove();
      $('#payment-method-cc .error').removeClass('error');
      $('#payment-method-cc ul').remove();
      ResetValidatious()
  
    } else {
      $('#NameOnCard').addClass('required');
      $('select[name=CreditCardType]').addClass('required');
      $('select[name=CardExpMonth]').addClass('required');
      $('select[name=CardExpYear]').addClass('required');
      $('#CardNumber').addClass('required');
      $('#CVC').addClass('required');
  
      $('#NameOnCard').attr('disabled', false);
      $('select[name=CreditCardType]').attr('disabled', false);
      $('select[name=CardExpMonth]').attr('disabled', false);
      $('select[name=CardExpYear]').attr('disabled', false);
      $('#CardNumber').attr('disabled', false);
      $('#CVC').attr('disabled', false);
  
      $('#customernumber').removeClass('required');
      $('#customernumber').attr('disabled', true);
      $('#payment-method-account .error').removeClass('error');
      $('#payment-method-account ul').remove();
      $('#payment-method-cc .error').removeClass('error');
      $('#payment-method-cc ul').remove();
      ResetValidatious();
    }
  }
  </code></pre>
  
  <p>Validation server side:</p>
  
  <pre><code>    if(form["purchaseorder"].Value == "yes") 
       {
           UserFormElement elementCustomerNumber = form.GetElement("CustomerNumber");
           if (null != elementCustomerNumber &amp;&amp; elementCustomerNumber.GetValue().Trim() == "")
           {
              elementCustomerNumber.AddError("&lt;br/&gt;Customer number is required for pre-approved account.");
           }
           if(form.HasError)
           {
             Redirect("checkout.aspx",true);
             return;
           }
  
          Payment payment = new Payment(CustomerCtxt);
          payment.PaymentTypeId = DMinSite.Common.DmiProxy.GetInterface&lt;DMinSite.Common.Interfaces.ILookupCommonService&gt;().GetLookupIdByValue(context, DMinSite.Common.Constants.LookupTypeEnum.PaymentType, "PO");
          PaymentPurchaseOrder paymentPurchaseOrder = new PaymentPurchaseOrder(context);
          paymentPurchaseOrder.Amount = cart.CartTotal;
          paymentPurchaseOrder.CustomerNumber = elementCustomerNumber.GetValue().Trim();
          payment.PaymentInfo = paymentPurchaseOrder;
  
          cart.AddPayment(payment);
      }
      else
      {
          CreditCard card = new CreditCard(CustomerCtxt);
          String note =  form.GetElement("Special").GetValue();
          note = note.Replace('"','\"');
          //order level note
          cart.Note = note;
  
          UserFormElement elementCCTypeId = form.GetElement("CreditCardType");
          card.CreditCardTypeId = elementCCTypeId.GetValue&lt;int&gt;();
  
          UserFormElement elementCCNumber = form.GetElement("CardNumber");
          card.Number = elementCCNumber.GetValue();
  
          UserFormElement elementCCName = form.GetElement("NameOnCard");
          card.NameOnCard = elementCCName.GetValue();
  
          UserFormElement elementCCExpYear = form.GetElement("CardExpYear");
          UserFormElement elementCCExpMonth = form.GetElement("CardExpMonth");
          if (elementCCExpMonth.GetValue().Trim() == "" || elementCCExpYear.GetValue().Trim() == "")
          {
              elementCCExpMonth.AddError("&lt;br/&gt;The expiration month and/or year is not valid.  Please check your selections and try again.");
          }
          else
          {
              card.ExpirationDate = ControlUtility.ConstructDateTime(
              elementCCExpYear.GetValue&lt;int&gt;(), 
              elementCCExpMonth.GetValue&lt;int&gt;(), 1);
          }
  
          UserFormElement elementCCCvv2 = form.GetElement("CVC");
          card.Cvv2 = elementCCCvv2.GetValue();
  
          //check fields
          if (elementCCNumber.GetValue().Trim() == "" || !card.IsValidCreditCardNumber(CustomerCtxt))
          {
              elementCCNumber.AddError("&lt;br/&gt;The credit card number is not valid.  Please check the number and try again.");
          }
          if (elementCCExpYear.GetValue().Trim() + elementCCExpMonth.GetValue().Trim() == "" || card.IsExpired(CustomerCtxt))
          {
              elementCCExpYear.AddError("&lt;br/&gt;The credit card has expired.  Please check the expiration date and try again.");
          }
          if (elementCCCvv2.GetValue().Trim() == "" || !card.IsValidCvv2(CustomerCtxt))
          {
              elementCCCvv2.AddError("&lt;br/&gt;The credit card CVV2 number is not valid.  Please check the CVV2 number and try again.");
          }
          if (elementCCName.GetValue().Trim() == "")
          {
              elementCCName.AddError("&lt;br/&gt;The credit card name is not filled out.  Please check the name and try again.");
          }
  
          if(form.HasError)
          {
             Redirect("checkout.aspx",true);
             return;
          }
  
          Payment payment = new Payment(CustomerCtxt);
          payment.PaymentTypeId = Core.GetLookupIdByValue("PaymentType", "CreditCard");
          payment.PaymentInfo = card;
          payment.Amount = cart.CartTotal;        
          cart.AddPayment(payment);
      }
  </code></pre>