title: JQuery Validation Ignore Form Fields With Default Form Field Values
body: <p>We have a form which uses the <strong>JQuery Validation Plugin</strong> and would like to <strong>ignore any form field values which have not been changed from their default values</strong>.</p>
  
  <p>For example, we have a credit card update form and only display the last four of the credit card number for security. It looks something like this:</p>
  
  <p><em><strong></em>**<em>*</em>*</strong> 5629</p>
  
  <p>This default value will be different for each user as the last four of their credit cards will be different.</p>
  
  <p>If the user does not alter their default credit card value during their profile update, we'd like to bypass the validation routing on this form field as obviously it would not validate as a valid credit card number.</p>
  
  <p>Here is our current validation snippet:</p>
  
  <pre><code>card_number: {
    required: true,
    creditcard: true
  }
  
  messages: {
    card_number: {
      required: "Please enter a &lt;strong&gt;valid credit card number&lt;/strong&gt;.",       
    }
  </code></pre>
  
  <p>And here is the form field:</p>
  
  <pre><code>&lt;input type="text" name="card_number" id="card_number" title="Your credit card number." value="************ 5629" /&gt;
  </code></pre>
  
  <p>How can we <strong>ignore validating any form field values which have not been changed from their default values</strong>?</p>
  
  <p>Thanks.</p>
  
  <p>===============================================</p>
  
  <p><strong>NEW NOTE TO ANDREW:</strong></p>
  
  <p>I think we're running into a little glitch due to the way in which we are validating the credit card TYPE.</p>
  
  <p>Here is the complete snippet along with your initial fix:</p>
  
  <pre><code>card_number: {
    creditcardtypes: function() {
      // return an object with a property named the same as the pay method
      var result = new Object();
      var propertyName = $('select[name="pay_method"]').val().toLowerCase();
      var propertyValue = 1;
      eval("result."+propertyName+"='"+propertyValue+"'");
      return result;
    },
    ccnotdefault: true,
    required: true
  },
  </code></pre>
  
  <p>How can we also accommodate the credit card TYPE rule? Right now, validation fails because the default <strong>credit card value is not a valid credit card type</strong>.</p>
  
  <p>Thanks again Andrew.</p>
  
  <p>================================================</p>
  
  <p><strong>FORM FIELDS:</strong></p>
  
  <pre><code>          &lt;!--- PAYMENT METHOD FIELD ---&gt;
        &lt;tr&gt;
          &lt;td style="padding-top:15px; text-align:right; width:45%;"&gt;&lt;label for="pay_method"&gt;&amp;nbsp;Payment Method:
            &lt;span class="orderlineredsm"&gt;&lt;strong&gt; * &lt;/strong&gt;&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
          &lt;td style="padding-top:15px; text-align:left; width:55%;" colspan="2" align="left" id="pmethod"&gt;
            &lt;select name="pay_method" id="pay_method" class="highlightme required" style="width: 115px;" onchange="jQuery('#card_number').valid()" title="Your Payment Method."&gt;
                &lt;option value="Visa" &lt;cfif form.pay_method EQ "Visa"&gt;selected="selected"&lt;/cfif&gt;&gt;Visa&lt;/option&gt;
                &lt;option value="Mastercard" &lt;cfif form.pay_method EQ "Mastercard"&gt;selected="selected"&lt;/cfif&gt;&gt;Mastercard&lt;/option&gt;
                &lt;option value="Amex" &lt;cfif form.pay_method EQ "Amex"&gt;selected="selected"&lt;/cfif&gt;&gt;Amex&lt;/option&gt;
                &lt;option value="Discover" &lt;cfif form.pay_method EQ "Discover"&gt;selected="selected"&lt;/cfif&gt;&gt;Discover&lt;/option&gt;
                &lt;option value="Diners" &lt;cfif form.pay_method EQ "Diners"&gt;selected="selected"&lt;/cfif&gt;&gt;Diners&lt;/option&gt;
            &lt;/select&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;!--- END PAYMENT METHOD FIELD ---&gt;
  
        &lt;!--- CARD NUMBER FIELD ---&gt;
        &lt;tr&gt;
          &lt;td style="text-align:right; width:45%;"&gt;&lt;label for="card_number"&gt;&amp;nbsp;Credit Card Number:
            &lt;span class="orderlineredsm"&gt;&lt;strong&gt; * &lt;/strong&gt;&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
          &lt;td style="text-align:left; width:55%;" colspan="2" align="left" id="cnumber"&gt;
            &lt;input type="text" name="card_number" id="card_number" class="highlightme" style="width: 130px;" maxlength="19" title="Your credit card number." value="************&lt;cfoutput&gt;#form.card_number#&lt;/cfoutput&gt;" onfocus="clearText(this)" onblur="clearText(this)" /&gt;
            &amp;nbsp;&lt;span class="create-tooltip" title="For your protection and security, only the &lt;strong&gt;last four digits of your credit card number&lt;/strong&gt; are shown.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Only change if you are updating your credit card number."&gt;&lt;img src="/public/images/help_25.png" style="margin-bottom:1px;" alt="help" width="25" height="25" /&gt;&lt;/span&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;!--- CARD NUMBER FIELD ---&gt;
  </code></pre>
  
  <p>==========================================================</p>
  
  <p><strong>POSSIBLE FIX</strong></p>
  
  <pre><code>card_number: {
    ccnotdefault: function(){ return $('#pay_method').val(); },
    required: true
  },
  
  jQuery.validator.addMethod("ccnotdefault", function(value, element, param) {
    // if the element is optional, don't validate 
    return this.optional(element) ||
    // or if the value is equal to the default value
    (value === element.defaultValue || 
    // or, finally, if the cc number is valid
    jQuery.validator.methods.creditcard2.call(this, value, element, param));
  }, jQuery.validator.messages.creditcard);
  
  http://www.ihwy.com/labs/downloads/jquery-validate-creditcard-2/jquery.validate.creditcard2-1.0.1.js
  </code></pre>