Regular expression for JavaScript
var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
var result = regPostcode.test(SearchTerm);
if (!result) {
alert(“Please enter valid postcode”);
return false;
}
Regular expression for C#
string regPostcode = “([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}”;
System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(regPostcode);
if(!regx.IsMatch(address0.PostalCode))
{
results.Add(new ValidationResult(String.Format(“Please enter valid postcode for {0}.”, addressRank)));
}
You can check the regular expression at http://www.regexplanet.com/
Postal code format and sample postcodes are as below.
Format | Example |
---|---|
A9 9AA | S1 1AA |
A99 9AA | M60 1NW |
AA9 9AA | CR2 6XH |
AA99 9AA | DN55 1PT |
A9A 9AA | W1A 1HQ |
AA9A 9AA | EC1M 1BB |