Creating Utility Class for Zip Code, Date and Integer Validation

Introduction
 
As mentioned in my previous blog, of creating Utility Class for Email ID and Phone Number Validation below is the code for validating Zip Code, Date and Integer.
  1. In AppCode Folder create a file named as AppUtility.
  2. Write the below mentioned code in the Utility page and access the same from any required page.

Validating Valid Zip Code.

public static Boolean IsValidZip(string strZip)
{
    Regex
objRegExp = new Regex("^[0-9]");
    Match
objMatch = objRegExp.Match(strZip);
    return
objMatch.Success;
}

Validating Valid Date.

public static Boolean IsValidDate(string strDOB)
{
    try

    {
         string
ValidDOB = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d";
         DateTime
Dt = new DateTime(Convert.ToInt16(strDOB.Substring(6, 4)), Convert.ToInt16(strDOB.Substring(3, 2)),

         Convert
.ToInt16(strDOB.Substring(0, 2)));

         DateTime
curDate = DateTime.Now;

         String
date="01/01/1900";
         DateTime
minDate=Convert.ToDateTime(date);
         Regex
objRegExp = new Regex(ValidDOB);
         Match
objMatch = objRegExp.Match(strDOB);
         if
(objMatch.Success)
        {
           if
(Dt > curDate)
           {
               return
false;
           }
          else
if (Dt < minDate)
          {
              return
false;
         }
         else

         {
             return
true;
         }
    }
    else

    {

         return
false;

    }
}
    catch

    {
        return
false;
    }
}

Validating Valid Integer

public static Boolean IsValidInteger(string strInt)
{

     Regex objRegExp = new Regex("^[0-9]");
     Match
objMatch = objRegExp.Match(strInt);
     if
(objMatch.Success)
     {
           int
value = Convert.ToInt32(strInt);
           //Max Integer Value is 32767

           if
(value <= 32767 && value >= 0)
           return
true;
           else
           return
false;
       }
       else
       return
false;
}

.AppUtility File 

AppUtility app = new AppUtility ();
If
(app.IsValidZip (txtZip.Text) || app.IsValidDate (txtDate.Text) || app.IsValidInteger (txtInteger.Text))
{
    Success = true
;
}

Ebook Download
View all
Learn
View all