Validate ZIP Code

This code sample shows how to validate a zip code using regulr expressions in Javascript.


function ValidateZip(s)
{
reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
if (!reZip.test(s)) {
alert("Zip Code Is In-Valid");
return false;
}
return true;
}


Comments

Author: Ultimaterengan10 Feb 2014 Member Level: Gold   Points : 3

try like this also
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;
}
}

more reference about this

http://www.dotnetspider.com/resources/22701-Validate-Zip-Code.aspx



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: