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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I stop user to enter Alphabets in numeric field

Status
Not open for further replies.

essa2000

Programmer
Dec 6, 2000
299
CA
Hi;

I am trying to stop user to type in a text box other than numeric values i.e. "1234567890.-" only !

I am using VB.NET version 2002.

Thanks.

Muhammad Essa Mughal
Software Engineer
iLogic Inc.
 
I use
Private Sub Number_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txtAmount.KeyPress, txtOrccNumber.KeyPress
Select Case e.KeyChar
Case Convert.ToChar(8) ' Backspace
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

Change the handles clause to include your control(s).

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
That is a good solution. But you may still find users who will copy text and paste it into your textbox.

If that is a concern, then I wouldn't prevent the user from doing so.

What I would do is check the values of your Textbox(es) before you process your function which commits the values. Use the IsNumeric(TextBox1.Text) function.

For example, don't process your save or whatever you have going on if there are errors, and indicate to the user which values are inacurrate.
 
Thanks , I was looking for the mentioned code , I already done that but I was unable to create logic for Backspace and you solved my problem. Right now , I am checking IsNumeric before calling my save functions.

Thanks.

Muhammad Essa Mughal
Software Engineer
iLogic Inc.
 
You can put that IsNumeric in the TextChanged event of your textbox as well. If the user copies/pastes text into the textbox, the changed event will fire and you can catch it there as well.

D'Arcy
 
faq796-3369.

You would need to change the longs for doubles.

Craig
 
Hi Craig;

I read that FAQ and I am trying to use it but I am wondering either I have to make user control or how can I use that class for any text box. Could you please explain how can I use it.

Right now , I am using JohnYingling's suggested code and it works fine and I added two more cases for "." and "-" in that code.

Thanks for your help.





Muhammad Essa Mughal
Software Engineer
iLogic Inc.
 
If your company is willing, Infragistics makes a great numeric control as well.

Since all the code to ensure numeric values is encapsulated within, there's less mess with having to re-invent the wheel.

Their grids are pretty good as well.

D
 
for pasting:

in the textboxes "textchanged" event you could simply run a validation of the text (just make a function to loop through every char and if 1 or more chars is not a # or a -then make it return a 0, or if its all good a 1)

---------------------------------------
HERES WHAT THE CODE WOULD BASICALLY BE:
---------------------------------------

Dim previoustxt as string = "" 'make this at the topmost part just after the class is declared


Private Sub textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox.TextChanged

if validatetxt(textbox.text) = 0 then textbox.text = previoustxt
previoustxt = textbox.text


End Sub


Private Function validatetxt(ByVal str As String) As Integer
Dim x

For x = 0 To Len(str) - 1
validatetxt = 0
If Char.IsDigit(str.Chars(x)) = True or mid(str, x + 1, 1) = "-" Then validatetxt = 1
If validatetxt = 0 Then Exit For
Next
End Function




that would prevent invalid text from being pasted in, i just gave you this code quick so if there are any problems just tell me. hope this helps
 
Thanks for giving me nice suggestions and now I got it ! I'll take care of Cut/copy and Paste functoin as well.

Now, I am using this function !

Public Function AllowNumeric(ByVal e As Object, Optional ByVal IsDecimalAllow As Boolean = True, Optional ByVal IsMinusSignAllow As Boolean = False)
Select Case e.KeyChar
Case Convert.ToChar(8) ' Backspace
Case "0" To "9"
Case IIf(IsDecimalAllow = True, ".", "")
Case IIf(IsMinusSignAllow = True, "-", "")
Case Else
e.Handled = True
End Select
End Function

I appreciate all the help which you all gave to me.
Thanks.





Muhammad Essa Mughal
Software Engineer
iLogic Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top