Validate postcode on keypress

http://stackoverflow.com/questions/27315788/validate-text-input-to-match-canadian-postal-code-e-g-a1s2s3

by Ketan Patel

HTML

<input type="text" id="postCode" placeholder="Enter postcode">
<span id="result"></span>

JavaScript

var pattern = /[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/,
$result = $("#result");

$('#postCode').keyup(function(){
  var val = this.value
  if(!val.match(pattern)){
    $result.text("invalid");
  } else {
    $result.text("valid");      
  }
});