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;


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;
txtZip.ForeColor = Color.Black;

more reference about this

  • 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: