Created
March 11, 2019 18:23
-
-
Save abdullahceylan/79e0bf10ef627ea7d7a4557e046c6118 to your computer and use it in GitHub Desktop.
U.K. Postcode Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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