title: Efficient regex for Canadian postal code function
body: <pre><code>var regex = /[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d/;
  var match = regex.exec(value);
  if (match){
      if ( (value.indexOf("-") !== -1 || value.indexOf(" ") !== -1 ) &amp;&amp; value.length() == 7 ) {
          return true;
      } else if ( (value.indexOf("-") == -1 || value.indexOf(" ") == -1 ) &amp;&amp; value.length() == 6 ) {
          return true;
      }
  } else {
          return false;
  }
  </code></pre>
  
  <p>The regex looks for the pattern A0A 1B1.
  true tests:</p>
  
  <p>A0A 1B1</p>
  
  <p>A0A-1B1</p>
  
  <p>A0A1B1</p>
  
  <p>A0A1B1C &lt;&lt; problem child</p>
  
  <p>so I added a check for "-" or " " and then a check for length.</p>
  
  <p>Is there a regex, or more efficient method?</p>