title: Javascript UK Postcode Validation
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive">UK Postcode Regex (Comprehensive)</a>  </p>
  </blockquote>
  
  
  
  <p>I have the following code for validating postcodes in javascript:</p>
  
  <pre><code>function valid_postcode(postcode) {
      postcode = postcode.replace(/\s/g, "");
      var regex = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
      return regex.test(postcode);
  }
  </code></pre>
  
  <p>Tests:</p>
  
  <pre><code>CF47 0HW - Passes - Correct
  CF47 OHW - Passes - Incorrect
  </code></pre>
  
  <p>I have done a ton of research but can't seem to find the definitive regex for this common validation requirement. Could someone point me in the right direction please?</p>