Just spent a while working out how to do this with the form Validation
plugin from http://bassistance.de/jquery-plugins/jquery-plugin-validation/
, but it's actually really logical when you get it. Still, might save
someone else the effort!

UK Postcodes have a fixed format and a standard regex to check against
[1], so you can check them simply, if you define a postcode method to
the validator plugin.

To do this:
<code>
jQuery.validator.addMethod("postcode", function(value) {
  return /\b([A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]?)
*[0-9][ABD-HJLN-UW-Z]{2}\b/.test(value);
  }, "Please specify a valid postcode (all letters should be
uppercase)");
}
</code>

Then, whenever you want to check against it:
<input name="postcode" class="{postcode:true}" />

Easy!
Not quite worked out how to check multiple inputs with the same name
though (as you get when you have a form that accepts an arbitrary
number of addresses, and normally handled in PHP by using names like
"postcode[]", putting the results in an array for use).
Any suggestions?

[1] /\b([A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]?) *[0-9]
[ABD-HJLN-UW-Z]{2}\b/ - technically, the space is required, but you
can always correctly determine the position if it's left out, so I
relaxed the server side processing on that point, and it would make
sense to relax it on the client side too.

Reply via email to