Skip to content

Instantly share code, notes, and snippets.

@abdullahceylan
Created March 11, 2019 18:23
Show Gist options
  • Save abdullahceylan/79e0bf10ef627ea7d7a4557e046c6118 to your computer and use it in GitHub Desktop.
Save abdullahceylan/79e0bf10ef627ea7d7a4557e046c6118 to your computer and use it in GitHub Desktop.
U.K. Postcode Validator
const postcodeRegex = /^[A-z]{1,2}[0-9Rr][0-9A-z]?[0-9][A-z]{2}$/;
const postcode = 'NG1 4UG';
const errors = {};
if (!postcode) {
errors.postcode = 'Please enter a postcode';
} else if (
!postcode
.replace(/\s/g, '')
.toUpperCase()
.match(postcodeRegex)
) {
errors.postcode = 'Please enter a full U.K. postcode';
};
console.log(errors.postcode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment