IsNumeric Function

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
 
D

Dan Bass

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.
 
H

Hans Kesting

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
 
G

Guest

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
 
G

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 :)

Thanks!
 
D

Dan Bass

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top