Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Alpha (A-Z) or Numeric input? How?

Status
Not open for further replies.

itdamon

MIS
Mar 18, 2002
69
0
0
US
1.) I need a way to validate that input in a text box is actually numeric, such as:

intUnits = CInt(txtUnits.Text)

That works with a Try/Catch but I want to validate that the input is actually numeric

2.) Is there a way to validate that input in a text box is only (A-Z)?

Sorry if these are newbie questions...I'm trying to make the transition from VB6 to VB.NET.

Thankx!
 
To check to see if numeric use something like
If IsNumeric(txtUnits.Text) Then
Some code
Else
messagebox.show("value must be numeric")
End If
Could place in LostFocus event so validates text box before moving on
 
Use the KeyDown event. (In .NET) e is the argument passed... in this case, what key was pressed. Type "e." and IntelliSense will bring up its list. There is a "KeyChar" or "KeyCode" property... something like that. That is the value of the key that triggered this event. You can check it against anything you want... if it's an invalid key for your context, set e.Handled to True and then Exit Sub. That will tell the program to ignore the changes that were made as a result of the key being pressed. It will look something like this:

If e.KeyChar <> &quot;1&quot;c Then
e.Handled = True
Exit Sub
End If

The implied &quot;else&quot; is empty (unless you need further validation.

You could even set up a string of valid characters and search for the KeyChar within that string... something like that will accomplish what you want.

Ben

There's no place like 127.0.0.1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top