IsNumeric Function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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
 
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
 
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!
 
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!
 
Back
Top