jQuery Validator Postcode Regex

Hi,

I have the following jQuery validator that validates a UK postcode:


jQuery.validator.addMethod("UKPostcode", function(value, element) {
	return this.optional(element) || /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/.test(value);
}, "Must be a valid Postcode");

This matches against for e.g OL15 1HG, BUT it doesn’t match against ol15 1hg. The problem is that it doesn’t accept lowercase characters. I thought I could just use [A-Za-z] but that didn’t work.

Any ideas?

Thanks :smiley:

You can tell the regular expression to ignore case, by placing the letter i after the closing delimiter for the regular expression.

Before:


/.../.test(value)

After:


/.../i.test(value)

Nice one, thanks for that. Worked a treat :slight_smile: