Javascript Regular Expressions Validate Postal Code

Description

Javascript Regular Expressions Validate Postal Code

function isValidPostalCode(postalCode) {
    let pcodeRegExp = /^(\d{5}(-\d{4})?|([a-z][a-z]?\d\d?|[a-z{2}\d[a-z])?\d[a-z][a-z])$/i;
    return pcodeRegExp.test( postalCode );     
}
let postalCode = "12345";

if (isValidPostalCode(postalCode)) {
    console.log("Valid Postal Code");
} else {//from w w  w.ja  v a  2s.  co  m
    console.log("Invalid Postal Code");
}



PreviousNext

Related