IsNumeric

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

    #1

    IsNumeric

    I need a way (Server Side) to check if a text box's value
    is numeric or not.

    Can anyone assist?

    Thanks

    Tom

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: IsNumeric

    Tom,

    If you want to use the IsNumeric function that comes with VB, then you
    can set a reference to Microsoft.Visua lBasic.dll in your project. IsNumeric
    is defined as a static method on the Information class in the
    Microsoft.Visua lBasic namespace.

    However, if you want a more ".NET" way (which isn't linked explicitly
    for VB, some people have issue with this), you can use the static TryParse
    method on the Double value type. This will attempt to parse a string, but
    you will have to be a little more specific about what is acceptable and what
    is not (through the use of number styles).

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Tom" <anonymous@disc ussions.microso ft.com> wrote in message
    news:009501c39a 3d$643abf00$a10 1280a@phx.gbl.. .[color=blue]
    > I need a way (Server Side) to check if a text box's value
    > is numeric or not.
    >
    > Can anyone assist?
    >
    > Thanks
    >
    > Tom
    >[/color]


    Comment

    • cody

      #3
      Re: IsNumeric

      you could do int.parse(textB ox.Text) and if no exception occures it is
      definitely a number.

      --
      cody

      Freeware Tools, Games and Humour

      [noncommercial and no fucking ads]


      Comment

      • Greg Ewing [MVP]

        #4
        Re: IsNumeric

        Tom, here are a couple of ways to do this:

        1 Use the Parse method on any of the Int32, Int64, etc classes. If it
        throws an exception it's not numeric.
        2. Add a reference to the Microsoft.Visua lBasic.dll assembly and then use
        the IsNumeric function on the Microsoft.Visua lBasic.Informat ion class.
        3. Write a regex statement that checks whether the string has anything but
        numbers.

        Hope that helps.

        --
        Greg Ewing [MVP]
        IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.




        "Tom" <anonymous@disc ussions.microso ft.com> wrote in message
        news:009501c39a 3d$643abf00$a10 1280a@phx.gbl.. .[color=blue]
        > I need a way (Server Side) to check if a text box's value
        > is numeric or not.
        >
        > Can anyone assist?
        >
        > Thanks
        >
        > Tom
        >[/color]


        Comment

        • Ignacio Machin \( .NET/ C#  MVP \)

          #5
          Re: IsNumeric

          Hi Tom,

          You could use a RegEx for this, here is one I'm using you may need to change
          it to your needs

          Regex r = new Regex(@"\d+|\d+ \.\d+");
          r.IsMatch( stringToTest );

          basically it will test true for a construction like XXX.YYY with any number
          of decimal places on each side of the "."

          You could also try to use Convert.ToDoubl e|ToInt32 , etc and put it inside a
          try/catch block

          Cheers,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation


          "Tom" <anonymous@disc ussions.microso ft.com> wrote in message
          news:009501c39a 3d$643abf00$a10 1280a@phx.gbl.. .[color=blue]
          > I need a way (Server Side) to check if a text box's value
          > is numeric or not.
          >
          > Can anyone assist?
          >
          > Thanks
          >
          > Tom
          >[/color]


          Comment

          • Tom

            #6
            Re: IsNumeric

            Ignacio Machin:

            Thanks for your reply. Can you tell me where I can get
            more information on regular expressions as your example is
            not exactly what I am looking for but close.

            I'm not familiar with Regex(@"\d+|\d+ \.\d+") this.

            Thanks
            Tom


            [color=blue]
            >-----Original Message-----
            >Hi Tom,
            >
            >You could use a RegEx for this, here is one I'm using you[/color]
            may need to change[color=blue]
            >it to your needs
            >
            >Regex r = new Regex(@"\d+|\d+ \.\d+");
            >r.IsMatch( stringToTest );
            >
            >basically it will test true for a construction like[/color]
            XXX.YYY with any number[color=blue]
            >of decimal places on each side of the "."
            >
            >You could also try to use Convert.ToDoubl e|ToInt32 , etc[/color]
            and put it inside a[color=blue]
            >try/catch block
            >
            >Cheers,
            >
            >--
            >Ignacio Machin,
            >ignacio.mach in AT dot.state.fl.us
            >Florida Department Of Transportation
            >
            >
            >"Tom" <anonymous@disc ussions.microso ft.com> wrote in[/color]
            message[color=blue]
            >news:009501c39 a3d$643abf00$a1 01280a@phx.gbl. ..[color=green]
            >> I need a way (Server Side) to check if a text box's[/color][/color]
            value[color=blue][color=green]
            >> is numeric or not.
            >>
            >> Can anyone assist?
            >>
            >> Thanks
            >>
            >> Tom
            >>[/color]
            >
            >
            >.
            >[/color]

            Comment

            • Ignacio Machin \( .NET/ C#  MVP \)

              #7
              Re: IsNumeric

              Hi Tom,

              You can get several books of regular expressions, in the MSDN you can also
              find some doc related to .NET implementation of it, if you haven;t do
              nothing with it I advise you to buy a book about them and read it first,
              they can get very complex :)

              Also take a look at this link, it's on JScript but the concepts are the same

              http://msdn.microsoft.com/library/de...xpressions.asp

              Cheers,

              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation

              "Tom" <anonymous@disc ussions.microso ft.com> wrote in message
              news:00ff01c39a 4a$c7d30a60$a30 1280a@phx.gbl.. .[color=blue]
              > Ignacio Machin:
              >
              > Thanks for your reply. Can you tell me where I can get
              > more information on regular expressions as your example is
              > not exactly what I am looking for but close.
              >
              > I'm not familiar with Regex(@"\d+|\d+ \.\d+") this.
              >
              > Thanks
              > Tom
              >
              >
              >[color=green]
              > >-----Original Message-----
              > >Hi Tom,
              > >
              > >You could use a RegEx for this, here is one I'm using you[/color]
              > may need to change[color=green]
              > >it to your needs
              > >
              > >Regex r = new Regex(@"\d+|\d+ \.\d+");
              > >r.IsMatch( stringToTest );
              > >
              > >basically it will test true for a construction like[/color]
              > XXX.YYY with any number[color=green]
              > >of decimal places on each side of the "."
              > >
              > >You could also try to use Convert.ToDoubl e|ToInt32 , etc[/color]
              > and put it inside a[color=green]
              > >try/catch block
              > >
              > >Cheers,
              > >
              > >--
              > >Ignacio Machin,
              > >ignacio.mach in AT dot.state.fl.us
              > >Florida Department Of Transportation
              > >
              > >
              > >"Tom" <anonymous@disc ussions.microso ft.com> wrote in[/color]
              > message[color=green]
              > >news:009501c39 a3d$643abf00$a1 01280a@phx.gbl. ..[color=darkred]
              > >> I need a way (Server Side) to check if a text box's[/color][/color]
              > value[color=green][color=darkred]
              > >> is numeric or not.
              > >>
              > >> Can anyone assist?
              > >>
              > >> Thanks
              > >>
              > >> Tom
              > >>[/color]
              > >
              > >
              > >.
              > >[/color][/color]


              Comment

              • Morten Wennevik

                #8
                Re: IsNumeric

                Seeing as noone mentioned this, and even though I'm not sure how it would
                perform ...

                string numeric = "0123456789 "
                string num = textBox.Text;
                for(int x = 0; x < num.Length; x++)
                {
                if( numeric.IndexOf (num[x]) == -1 )
                {
                // not a number, one of the characters was not 0 1 2 3 4 5 6 7 8 9
                }
                }

                If you want a decimal point, add "." or "," to numeric.
                Use a bool flag to make sure there is only one.

                --
                Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

                Comment

                Working...