How to add only numeric values to a textbox control
-
Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi
-
Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi
Hi, I don't know if this is the best way of doing it, but you could capture the textbox on change event and then loop through each char in the text field to check if it is a number or not. So something like this:
Public Sub OnTextChange(Byval sender as Object, Byval e EventArgs) Handels T.TextChanged for each c as Char in T.Text.Chars if Not c.IsNumber Then MessageBox.Show("Bla bla bla") end if Next End Sub
Hope this helps /Johan -
Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi
u can also do this on keypress event of text box Private Sub txt1_KeyPress(KeyAscii As Integer) dim strValid as string strValid = "1234567890" if instr(strValid , chr(keyAscii)) = 0 then keyAscii = 0 end if end sub It would help u not to enter any character other than numeric values Noshaba
-
Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi
Rather than annoy the user with a popup message which theyneed to discard every time they type an incorrect character, you should really restrict the characters that can be entered in the first place. There are plenty of articles/posts on this subject here at CP if you search.
-
Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi
-
Hello EveryOne, How to allow only numeric values ina textbox.if we allow characters it should pop up a message showing "need to enter numeric values". Please HElp Regarding this, Thanks Nagalakshmi
One of easy ways....:)
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If (AscW(e.KeyChar) < 48 Or AscW(e.KeyChar) > 57) And Not e.KeyChar = Chr(Keys.Back) Then e.KeyChar = Nothing End If End Sub
Regards,
Chatura Dilan
-
Rather than annoy the user with a popup message which theyneed to discard every time they type an incorrect character, you should really restrict the characters that can be entered in the first place. There are plenty of articles/posts on this subject here at CP if you search.
I built a little function: function CashVal (byRef Intext as object, byRef ErrInd as boolean) as double 1. Check Intext to be sure it's not nothing 2. Check Intext to be sure it's not null 3. Check Intext to be sure it's numeric (and in my case, > 0...) 4. Return CDbl(Intext) and an appropriate ErrInd. I invoke this (x = CashVal(textboxname.text,Error_Indicator) )when the "leave" event is fired, so the user can type anything, and it's evaluated as a whole. The event handler checks to see if there was an error. If not, it sets the text property of the text box to format (x, "currency"). If there's an error, obviously, it can react appropriately.