(Struggled with this … thought others may benefit)
- Data entry field = zip_code; no mask. Min = 0 / Max = 10. Uppercase. Allowed characters = letters, numbers, space and (more) hyphen.
- (These are the rules I needed) Valid zip codes may be 5 characters numeric (USA), 9 characters numeric (USA) - with hyphen between 5th & 6th characters, or 6 characters (Canada) - may or may not be separated by space between 3rd & 4th character - cannot begin with W or Z and also cannot contain D, F, I, O, Q or U.
- Events > OnValidate = validate_postal_code() ;
- PHP Method (validate_postal_code) = if(preg_match("/^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/i",$zip_code))
return true;
else
sc_error_message(“Not a valid zipcode”) ;
NOTE: Other country regex values available on the internet if you look for them (all I needed was USA/Canada). Would have to change this to an if-then-else statement or switch for multiple countries. Tried to do this using a library and zip_code_onblur ajax event but could not make that work (I am relatively new to SC and don’t have that down yet)