Integer Type (.net)

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

    Integer Type (.net)

    If I have a textbox, Let use to input a text.
    I want to check the input text is integer type or no
    I use isnum() to validate it, but double type also pass the validation.
    I don't want to use Field Validation (asp.net) to check it

    Any Function can check the input type is integer or not
    Thank You X 100!!
  • Ken Cox [Microsoft MVP]

    #2
    Re: Integer Type (.net)

    You can use Cint() for this. It will throw an OverflowExcepti on if the value
    is out of range. For example, in this code try the value 2147483648 which is
    one greater than the max for an integer:

    Private Sub Button1_Click _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArg s) _
    Handles Button1.Click
    Dim intTest As Integer
    Try
    intTest = CInt(TextBox1.T ext)
    Label1.Text = intTest.ToStrin g
    Catch ex As OverflowExcepti on
    Label1.Text = "That was not an integer"
    End Try
    End Sub

    <form id="Form1" method="post" runat="server">
    <P>
    <asp:TextBox id="TextBox1" runat="server"> 2147483648</asp:TextBox></P>
    <P>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
    <P>
    <asp:Label id="Label1" runat="server"> </asp:Label></P>
    </form>

    "FatboyCant een" <anonymous@disc ussions.microso ft.com> wrote in message
    news:D74A2F32-BF68-48E7-BA34-8BA42E9FDE2D@mi crosoft.com...[color=blue]
    > If I have a textbox, Let use to input a text..
    > I want to check the input text is integer type or not
    > I use isnum() to validate it, but double type also pass the validation..
    > I don't want to use Field Validation (asp.net) to check it.
    >
    > Any Function can check the input type is integer or not?
    > Thank You X 100!![/color]

    Comment

    • Morgan

      #3
      Re: Integer Type (.net)

      Could you not use a CompareValidato r and set the type to Compare(or datatype
      check, can't remember the specifics) and set the datatype to Integer? That
      would prevent a postback, but requires additional space in the form
      designer.

      Morgan

      "FatboyCant een" <anonymous@disc ussions.microso ft.com> wrote in message
      news:D74A2F32-BF68-48E7-BA34-8BA42E9FDE2D@mi crosoft.com...[color=blue]
      > If I have a textbox, Let use to input a text..
      > I want to check the input text is integer type or not
      > I use isnum() to validate it, but double type also pass the validation..
      > I don't want to use Field Validation (asp.net) to check it.
      >
      > Any Function can check the input type is integer or not?
      > Thank You X 100!![/color]


      Comment

      • Kevin Spencer

        #4
        Re: Integer Type (.net)

        Or, if you're using C#, you can use Convert.ToInt32 (), which also works with
        VB.Net.

        --
        HTH,
        Kevin Spencer
        ..Net Developer
        Microsoft MVP
        Big things are made up
        of lots of little things.

        "Ken Cox [Microsoft MVP]" <BANSPAMken_cox @sympatico.ca> wrote in message
        news:#HOovGU5DH A.504@TK2MSFTNG P11.phx.gbl...[color=blue]
        > You can use Cint() for this. It will throw an OverflowExcepti on if the[/color]
        value[color=blue]
        > is out of range. For example, in this code try the value 2147483648 which[/color]
        is[color=blue]
        > one greater than the max for an integer:
        >
        > Private Sub Button1_Click _
        > (ByVal sender As System.Object, _
        > ByVal e As System.EventArg s) _
        > Handles Button1.Click
        > Dim intTest As Integer
        > Try
        > intTest = CInt(TextBox1.T ext)
        > Label1.Text = intTest.ToStrin g
        > Catch ex As OverflowExcepti on
        > Label1.Text = "That was not an integer"
        > End Try
        > End Sub
        >
        > <form id="Form1" method="post" runat="server">
        > <P>
        > <asp:TextBox id="TextBox1" runat="server"> 2147483648</asp:TextBox></P>
        > <P>
        > <asp:Button id="Button1" runat="server"[/color]
        Text="Button"></asp:Button></P>[color=blue]
        > <P>
        > <asp:Label id="Label1" runat="server"> </asp:Label></P>
        > </form>
        >
        > "FatboyCant een" <anonymous@disc ussions.microso ft.com> wrote in message
        > news:D74A2F32-BF68-48E7-BA34-8BA42E9FDE2D@mi crosoft.com...[color=green]
        > > If I have a textbox, Let use to input a text..
        > > I want to check the input text is integer type or not
        > > I use isnum() to validate it, but double type also pass the validation..
        > > I don't want to use Field Validation (asp.net) to check it.
        > >
        > > Any Function can check the input type is integer or not?
        > > Thank You X 100!![/color]
        >[/color]


        Comment

        • Brett J

          #5
          Re: Integer Type (.net)

          Another way to test would be to use a regular expression. In general,
          try/catch blocks shouldn't be used for controlling program flow, but in
          this case it probably isn't a big deal, especially if most of the time
          the numbers entered will truly be integers. If they are not and the
          exception must be caught, there is a lot of overhead associated with
          this. However, if only for the sake of avoiding bad habits, I would use
          a regex approach. The following (C#) method should work for you:

          private bool IsInteger(strin g test)

          {
          Regex reg = new Regex(@"^[-+]?[1-9]\d*$");
          Match mat = reg.Match(test) ;
          return mat.Success;
          }

          *NOTE:
          If you don't want to accept negative numbers, remove the [-+]?
          If you want to accept numbers that end with a '.0' as an integer you
          could use: ^[-+]?[1-9]\d*\.?[0]*$

          Regular expression curtesy of Chuck Scholton
          (http://regexplib.com/REDetails.aspx?regexp_id=268)


          Best,
          Brett


          "=?Utf-8?B?RmF0Ym95Q2F udGVlbg==?=" <anonymous@disc ussions.microso ft.com>
          wrote in news:D74A2F32-BF68-48E7-BA34-8BA42E9FDE2D@mi crosoft.com:
          [color=blue]
          > If I have a textbox, Let use to input a text..
          > I want to check the input text is integer type or not
          > I use isnum() to validate it, but double type also pass the
          > validation.. I don't want to use Field Validation (asp.net) to check
          > it.
          >
          > Any Function can check the input type is integer or not?
          > Thank You X 100!![/color]

          Comment

          Working...