Saturday, April 11, 2009

Validate Zip Code

The following pattern checks whether the input string is a valid zipcode in the format ddddd or ddddd-dddd, where d is any digit 0-9.


private void btnValidate_Click(object sender, System.EventArgs e)
{
bool IsValid = true;
if (!Regex.IsMatch(txtZip.Text, @"^\s*(\d{5}|(\d{5}-\d{4}))\s*$"))
{
txtZip.ForeColor = Color.Red;
IsValid = false;
}
else
{
txtZip.ForeColor = Color.Black;
}
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home