IsNumeric Function

Discussion in 'ASP .Net' started by =?Utf-8?B?TXJNaWtl?=, Nov 29, 2004.

  1. Hi. I'm trying to create a simple validation process that only allows a user
    to enter a numeric value into a textbox. However, my process does not work
    and the user can still enter non-numeric values. My code is below. How can
    I fix this? Thanks.

    If Not (IsNumeric(CType(e.Item.Cells(3).Controls(0), TextBox).Text)) Then
    Me.lblErrorMsg.Text = "Only numeric values allowed!"
    Exit Sub
    End If
     
    =?Utf-8?B?TXJNaWtl?=, Nov 29, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?TXJNaWtl?=

    Dan Bass Guest

    Mike,

    A suggestion would be to scrap this code behind and go with client side
    validation. What you're doing isn't wrong, but it does mean a post-back has
    to a occur before the user will see their error. It's better to have a
    CompareValidator that contains your text error string, where the operator =
    "DataTypeCheck" and the Type is "Integer"...

    This will only allow a postback if the value in the textbox is an integer.

    Google on CompareValidator for more, or let me know if you need more help.

    Daniel.


    "MrMike" <> wrote in message
    news:...
    > Hi. I'm trying to create a simple validation process that only allows a
    > user
    > to enter a numeric value into a textbox. However, my process does not
    > work
    > and the user can still enter non-numeric values. My code is below. How
    > can
    > I fix this? Thanks.
    >
    > If Not (IsNumeric(CType(e.Item.Cells(3).Controls(0), TextBox).Text)) Then
    > Me.lblErrorMsg.Text = "Only numeric values allowed!"
    > Exit Sub
    > End If
     
    Dan Bass, Nov 29, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?TXJNaWtl?=

    Hans Kesting Guest

    MrMike wrote:
    > Hi. I'm trying to create a simple validation process that only
    > allows a user to enter a numeric value into a textbox. However, my
    > process does not work and the user can still enter non-numeric
    > values. My code is below. How can I fix this? Thanks.
    >
    > If Not (IsNumeric(CType(e.Item.Cells(3).Controls(0), TextBox).Text))
    > Then Me.lblErrorMsg.Text = "Only numeric values allowed!"
    > Exit Sub
    > End If


    Use a "CompareValidator", with type set to integer and operator
    set to datatypecheck. Don't forget the "ControlToValidate" :)


    Hans Kesting
     
    Hans Kesting, Nov 29, 2004
    #3
  4. Mike,
    Remember, even though you wrote this code:
    Me.lblErrorMsg.Text = "Only numeric values allowed!"

    You still don't through an exception, therefore this will still cause the
    form to post its values. Like the others explained, use the asp.net
    validation controls to stop users from entering bad values

    "MrMike" wrote:

    > Hi. I'm trying to create a simple validation process that only allows a user
    > to enter a numeric value into a textbox. However, my process does not work
    > and the user can still enter non-numeric values. My code is below. How can
    > I fix this? Thanks.
    >
    > If Not (IsNumeric(CType(e.Item.Cells(3).Controls(0), TextBox).Text)) Then
    > Me.lblErrorMsg.Text = "Only numeric values allowed!"
    > Exit Sub
    > End If
     
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=, Nov 29, 2004
    #4
  5. Thank you all for your posts. I've successfully implemented a
    comparevalidator as suggested. I'm still making the transition from Access
    VBA to VB.NET :)

    Thanks!

    "Tampa .NET Koder" wrote:

    > Mike,
    > Remember, even though you wrote this code:
    > Me.lblErrorMsg.Text = "Only numeric values allowed!"
    >
    > You still don't through an exception, therefore this will still cause the
    > form to post its values. Like the others explained, use the asp.net
    > validation controls to stop users from entering bad values
    >
    > "MrMike" wrote:
    >
    > > Hi. I'm trying to create a simple validation process that only allows a user
    > > to enter a numeric value into a textbox. However, my process does not work
    > > and the user can still enter non-numeric values. My code is below. How can
    > > I fix this? Thanks.
    > >
    > > If Not (IsNumeric(CType(e.Item.Cells(3).Controls(0), TextBox).Text)) Then
    > > Me.lblErrorMsg.Text = "Only numeric values allowed!"
    > > Exit Sub
    > > End If
     
    =?Utf-8?B?TXJNaWtl?=, Nov 29, 2004
    #5
  6. =?Utf-8?B?TXJNaWtl?=

    Dan Bass Guest


    > Thank you all for your posts. I've successfully implemented a
    > comparevalidator as suggested. I'm still making the transition from
    > Access
    > VBA to VB.NET :)


    Good to ask questions, even better to listen to the answers. Looking at the
    guys around these parts, I realise I know's nothing!
     
    Dan Bass, Nov 29, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Bour

    IsNumeric C# equivalent

    Stephan Bour, Nov 5, 2003, in forum: ASP .Net
    Replies:
    12
    Views:
    243,976
    daniefvh
    Jan 26, 2012
  2. martin
    Replies:
    3
    Views:
    10,623
    Kevin Spencer
    Jul 19, 2004
  3. =?Utf-8?B?Sm9l?=

    .NET IsNumeric function

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    25,404
    Michael D. Ober
    Feb 8, 2006
  4. Ulf Meinhardt
    Replies:
    0
    Views:
    1,021
    Ulf Meinhardt
    Aug 11, 2009
  5. bizt
    Replies:
    13
    Views:
    284
    Curtis
    Sep 18, 2008
Loading...

Share This Page