I'm currently working on a function that validates postcodes using a regex. The function should be able to handle either a single postcode or multiple postcodes.
When I input a single postcode, everything works as expected. However, when I try to input multiple postcodes, I only receive 'undefined' as a result.
Below is my function:
const validatePostcode = (...postcode) => {
const postcodeRegex = /^[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}$/i;
if (postcode.length > 1) {
postcode.forEach((item) => {
console.log(item);
return postcodeRegex.test(item);
});
} else {
return postcodeRegex.test(postcode);
}
};