I’ve been using this regular expression for validating UK postcodes in my contact forms and it works fine:
/^([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}$/
however I now need it to be modified to validate with or without a space e.g. GL50 1HX or GL501HX
Is there an easy solution to this?
To specify an optional space in a regular expression, put a question mark after it.
Example:
The question mark is the short form for the
{0,1}qualifier, i.e. zero or one times: