Another week, another validation function. Last week was a little long, so this week we'll do a bit shorter one: validating a United States ZIP code. The pattern is simple enough that I don't think an explanation is warranted.
ASP
function isValidZIPCode(zipCode)dim regExset regEx = new RegExpwith regEx.IgnoreCase = True.Global = True.Pattern = "^[0-9]{5}(-[0-9]{4})?$"end withif regEx.Test(trim(CStr(zipCode))) thenisValidZipCode = TrueelseisValidZipCode = Falseend ifset regEx = nothingend function
PHP
function isValidZIPCode($zipCode){return (preg_match("/^[0-9]{5}(-[0-9]{4})?$/i", trim($zipCode)) > 0) ? true : false;}
No comments:
Post a Comment