Jump to content
Xtreme .Net Talk

How do I prevent user from entering strings other than numeric onto the textbox?


Recommended Posts

Posted

Dear all,

 

How do I do that? To prevent user from entering "abcd", the user may only enter "1234".....

Say if the user enter something that's not numeric, then the textbox won't response....

 

Thanks

 

PlayKid

Posted

 Private Sub txtCalledNumber_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles txtCalledNumber.KeyPress
       If Not Char.IsNumber(e.KeyChar) And Not Asc(e.KeyChar) = Keys.Back Then
           e.Handled = True
       End If
   End Sub  

 

kEYS.BACK is for the back space. That way if you make a mistake you can still use the backspace key

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
  • *Experts*
Posted

If you use techmanbd's code and you want the user to be able to enter decimals, then make sure you allow the "." character as well.

 

Or just skip all the validation and use a NumericUpDown control.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...