Validating a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Griffin

    Validating a string?

    I am looking for a good referance on how to validate a string in C#.
    Specifically have a string which will contain a postal code that may or may
    not be in the US. I need to check this string to decide wheather it is in
    the US or outside the US. I think all I really need to do is see if the
    string meets the form of five integers. If it does then I will assume that
    it is a US zipcode and if it does not then I will assume that it is outside
    the US.

    Anyways sorry for the long explination but all I really need is a good
    referance on how to check the string to see if it is 5 integers or somthing
    else.

    Thanks, Jeff Griffin


  • Frank Oquendo

    #2
    Re: Validating a string?

    using System;
    using System.Text.Reg ularExpressions ;

    namespace ConsoleApplicat ion4
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    Console.Write(" Enter a zip code: ");
    string input = Console.ReadLin e();
    // Be sure to allow for ZIP + 4 as well
    Match match = Regex.Match(inp ut, @"^\d{5}(-\d{4})?$");
    Console.WriteLi ne(match.Succes s);
    Console.ReadLin e();
    }
    }
    }

    --
    There are 10 kinds of people. Those who understand binary and those who
    don't.




    Comment

    • Sunny

      #3
      Re: Validating a string?

      In article <uZDa9PfgDHA.16 08@TK2MSFTNGP12 .phx.gbl>, sfajeff2
      @hotmail.NOSPAM .com says...[color=blue]
      > I am looking for a good referance on how to validate a string in C#.
      > Specifically have a string which will contain a postal code that may or may
      > not be in the US. I need to check this string to decide wheather it is in
      > the US or outside the US. I think all I really need to do is see if the
      > string meets the form of five integers. If it does then I will assume that
      > it is a US zipcode and if it does not then I will assume that it is outside
      > the US.
      >
      > Anyways sorry for the long explination but all I really need is a good
      > referance on how to check the string to see if it is 5 integers or somthing
      > else.
      >
      > Thanks, Jeff Griffin
      >
      >
      >[/color]
      Hi Jeff,
      I'll write some code in here, so you may check it for typos.

      bool CheckZIP(string sZip)
      {
      bool bSuccess = false;

      if (sZip.Lenght == 5)
      {
      int i;
      try
      {
      i = Int32.Parse(sZi p); //contains only digits
      //if we reach here, the string contains only
      //digits, else, we'll be in the catch block
      bSuccess = true;
      }
      catch
      {
      //nothing to do, just avoid exception
      //the bSuccess is already false
      }
      }

      return bSuccess;
      }

      Hope that helps.
      Sunny

      Comment

      • Bruno Jouhier [MVP]

        #4
        Re: Validating a string?

        Zip codes are 5 integers in France, and probably in lots of other countries.
        So, you should not use this criteria to infer the country. You should add
        some UI to let the user select the country (if your app has a UI, of
        course).

        Otherwise the System.Text.Reg ularExpression is a good place to start, or you
        could write a specialized method that tests the string length and its
        contents.

        static bool IsZip5(string s)
        {
        if (s.Length != 5)
        return false;
        foreach (char ch in s)
        {
        if (!Char.IsDigit( ch))
        return false;
        }
        return true;
        }

        Bruno.

        "Jeff Griffin" <sfajeff2@hotma il.NOSPAM.com> a écrit dans le message de
        news:uZDa9PfgDH A.1608@TK2MSFTN GP12.phx.gbl...[color=blue]
        > I am looking for a good referance on how to validate a string in C#.
        > Specifically have a string which will contain a postal code that may or[/color]
        may[color=blue]
        > not be in the US. I need to check this string to decide wheather it is in
        > the US or outside the US. I think all I really need to do is see if the
        > string meets the form of five integers. If it does then I will assume that
        > it is a US zipcode and if it does not then I will assume that it is[/color]
        outside[color=blue]
        > the US.
        >
        > Anyways sorry for the long explination but all I really need is a good
        > referance on how to check the string to see if it is 5 integers or[/color]
        somthing[color=blue]
        > else.
        >
        > Thanks, Jeff Griffin
        >
        >[/color]


        Comment

        • Sunny

          #5
          Re: Validating a string?

          In article <Oa7YoYfgDHA.24 08@TK2MSFTNGP09 .phx.gbl>, franko@acadx.co m
          says...[color=blue]
          > using System;
          > using System.Text.Reg ularExpressions ;
          >
          > namespace ConsoleApplicat ion4
          > {
          > class Class1
          > {
          > [STAThread]
          > static void Main(string[] args)
          > {
          > Console.Write(" Enter a zip code: ");
          > string input = Console.ReadLin e();
          > // Be sure to allow for ZIP + 4 as well
          > Match match = Regex.Match(inp ut, @"^\d{5}(-\d{4})?$");
          > Console.WriteLi ne(match.Succes s);
          > Console.ReadLin e();
          > }
          > }
          > }
          >
          >[/color]
          Perfect, I have to read more about that regex stuff ...

          So, discard my other post :)

          Sunny

          Comment

          • Jeff Griffin

            #6
            Re: Validating a string?

            Frank, Sunny, Bruno- Thank yall so much, that was exactly what I needed.

            Frank - Do you know of a site (or book) that explains more about the
            following and what it all means:

            @"^\d{5}(-\d{4})?$");

            This seems to work great for what I need, but I would like to learn more
            about what all this means. I am in college and have not learned about any of
            this yet and would like to learn some more if I ever need to use this kind
            of thing again.

            Bruno - That is a good point, I was unaware that france used 5 digit
            zipcodes as well. I have been mostly dealing with Canada and the UK. If
            anyone else reads this and is trying to do somthing similar I was able to
            find a solution to this by calling a web service that validates a US zipcode
            if it meets th 5 integer criteria. I know I could have just done this to
            begin with to check, but since it is a webservice it produces a perforamance
            penalty, thus I find it better to do the check for 5 integers first and then
            if it meets that criteria I check it aginst the webservice. This negates the
            performance penalty for those with a "zipcode" that does not conform to the
            5 integer rule.

            Again, Thank you all very much!

            Jeff Griffin


            Comment

            • Frank Oquendo

              #7
              Re: Validating a string?

              Thus spake Jeff Griffin:
              [color=blue]
              > Frank - Do you know of a site (or book) that explains more about the
              > following and what it all means:
              >
              > @"^\d{5}(-\d{4})?$");[/color]

              Do what I did: use Google. ;)
              [color=blue]
              > This seems to work great for what I need, but I would like to learn
              > more about what all this means. I am in college and have not learned
              > about any of this yet and would like to learn some more if I ever
              > need to use this kind of thing again.[/color]

              A search on "c# regex tutorial" yielded dozens of pages on the subject.

              --
              There are 10 kinds of people. Those who understand binary and those who
              don't.




              Comment

              • Jeff Griffin

                #8
                Re: Validating a string?

                Just what I needed, Thanks Frank! (I was trying to look up "validate C#
                string" which returned some allright results, but a lot of the same article
                over and over again, just a different sites).

                Jeff Griffin


                Comment

                • David Gutierrez[MSFT]

                  #9
                  Re: Validating a string?

                  For a good book on Regular Expressions, take a look at "Mastering Regular
                  Expressions" by Jeffrey Freidl.
                  David

                  Comment

                  Working...