I have a pattern match here that looks like it should work fine. However any input I give makes the conditional fail. I will handle the '99999-9999' case after I get the '99999' case working.
$ZipCode is a textfield that is being submitted on POST.
$ZipCode = $_POST["ZipCode"];
if(!preg_match("/^[0-9]{5}$/", $ZipCode))
{$error_str .= "The zip code you enter must be in the form of: '99999' or '99999-9999'\n";}
if(isset($_POST['submit']))
{?><script>var error = <?= json_encode($error_str);?>;
alert(error);
</script>
<?}
'11111' fails and '111111' also fails