Important alert: (current site time 7/15/2013 7:40:36 AM EDT)
 

VB icon

Accept numeric values only in a textbox

Email
Submitted on: 1/30/2003 12:39:09 AM
By: Tristan B. Astillero 
Level: Intermediate
User Rating: By 21 Users
Compatibility: VB.NET
Views: 46818
(About the author)
 
     It validates the entries of a textbox while it is being entered, and allows only one instance of the decimal point(period) to be entered. Please vote and leave a comment so that at least i know im doing something right. =)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Accept numeric values only in a textbox
// Description:It validates the entries of a textbox while it is being entered, and allows only one instance of the decimal point(period) to be entered. Please vote and leave a comment so that at least i know im doing something right. =)
// By: Tristan B. Astillero
//
// Inputs:It has 2 parameters: e.keychar(eventargs) and the textbox control.
//
// Returns:Returns a boolean value indicating if the event is handled or not.
//
// Assumes:If you have any suggestions or encounter an error I would appreciate your comments. Thanks. I it's simple but hopefully this will garner me some votes. =P
//
// Side Effects:None to my knowledge(so far).
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=932&lngWId=10//for details.//**************************************

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
 e.Handled = NumbersOnly(e.KeyChar, TextBox1)
 End Sub
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
 'validate the entry for a textbox limiting it to only numeric values and the decimal point
 If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
 If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
 Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
 End If
 Return False 'for backspace
 End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/31/2003 1:03:38 PM

Your way to verify the values is far more elegant than mine. Well done!
(If this comment was disrespectful, please report it.)

 
3/25/2003 7:47:55 PM

Good, simple code.

There's a few implicit conversions that cause problems when Option Explicit is ON.

You should correct your code accordingly.

(If this comment was disrespectful, please report it.)

 
7/6/2003 1:47:18 AMcrouchie1998

This code isn't anything new. Its documented in a few VB.NET Books. However, this code isn't in a user control, whereas the code in the books is.
(If this comment was disrespectful, please report it.)

 
7/29/2003 9:25:49 PM

KeyPress doesn't stop a user from pasting an invalid value into the textbox!
(If this comment was disrespectful, please report it.)

 
8/7/2003 9:19:11 AM

Tks it helped me ! =)
(If this comment was disrespectful, please report it.)

 
10/4/2003 9:39:12 AMcoder100

can't we just use the NumericTextBox control?
(If this comment was disrespectful, please report it.)

 
12/2/2003 2:43:16 PM

I thing you should use a Regular Expresion Validator.
(If this comment was disrespectful, please report it.)

 
1/7/2004 7:11:26 PM

IsNumeric(TextBox1.Text)
Format(Val(textbox1.text), "###0.0")
Should do the trick also
(If this comment was disrespectful, please report it.)

 
1/14/2004 1:33:18 AMTristan B. Astillero

tnx for d feedback every1. will try to improve it wen i get 2 do some .net agen
(If this comment was disrespectful, please report it.)

 
2/9/2004 11:03:02 PMpcito

How about some good, old fashioned indentation? Otherwise, good code.
(If this comment was disrespectful, please report it.)

 
2/23/2004 4:32:17 AMTristan B. Astillero

tnx for ur feedback every1. to answer some of the posts, this code was done by a beginner in .net, i didn't know they had it in books. it works only when typing in the values, NOT pasting it in. i'd only realized that i cn use it in a control after i'd finished d proj(meaning, rush project). i tried indenting d code but it still came out like that.
(If this comment was disrespectful, please report it.)

 
4/20/2004 1:58:08 PMCrZ

This code does not work
'Handled' is not a member of 'System.EventArgs'.

(If this comment was disrespectful, please report it.)

 
8/30/2004 2:20:06 PMNigel Hooper

How about making this code culturally aware by using the current Number Decimal Separator, instead of hard coding it? (see System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) or at least make the
(If this comment was disrespectful, please report it.)

 
8/30/2004 2:21:20 PMNigel Hooper

or at least make the "." a constant / variable.
(If this comment was disrespectful, please report it.)

 
8/9/2005 6:25:25 AMSudha

Well Done. If more explanation could be given beginers will understand it better
(If this comment was disrespectful, please report it.)

 
3/31/2006 2:14:13 PMCarlos

Good Job my Friend
(If this comment was disrespectful, please report it.)

 
4/12/2007 1:22:04 AM

I love you...
(If this comment was disrespectful, please report it.)

 
1/29/2008 12:05:45 AMwilliams

i think that a simple method is already exist my friend..
(If this comment was disrespectful, please report it.)

 
10/13/2010 3:59:40 PMCory S.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "." Then
Dim r = TextBox1.Text.IndexOf(".")
If r > 0 Then
e.KeyChar = ""
Else
e.KeyChar = "."
End If
ElseIf IsNumeric(e.KeyChar) = False Then
e.KeyChar = ""
End If
End Sub

another version... good beginner code. everybody that is leaving BS comments, realize this is a website where a lot of beginners go to learn 'basic' programming. If you have a better way, show it.. commenting saying there is a better way with out showing anything doesnt help any body
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.