/    Sign up×
Community /Pin to ProfileBookmark

UK Postcode Validation

Hello,

I am trying to validate the first part of a uk postcode(post out) which has the following permetations AN / AAN / ANN / AANN. A(ALPHA) N(nUMERIC). I have managed to find a regular expression on a forum based on the the full uk postcode not just my requirement of the first part as with the above permetations. As i am not 100% sure of the expression’s syntax i was wondering if anyone could alter it to test only for the first part with the above permetations??

function isValidPostcode(postcode) {
if (!/^[A-Z]{1,2}d{1,2} d[A-Z]{2}$/.test(postcode)) {
return false;
}
return true;
}

Thanks in advance

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 07.2005 — If you just pass the first part:
if (!/^[A-Z]{1,2}d{1,2}$/i.test(postcode)) {
Copy linkTweet thisAlerts:
@karlmcauleyauthorJan 07.2005 — Cheers for that.

I just did not understand the syntax.

Karl
×

Success!

Help @karlmcauley spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...