Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Validate Text fields for Numeric Data

Author: DevASP
Download Source Code : 320_OnlyNumerics.zip

In this article I will try to explain you how you can validate your TextFields for numeric values like cell number etc.

In some situations you want that the user should enter only numeric values in the textbox. For example when you are asking the user to enter his/her phone number or his/her age. Here is the technique the will help you out in performing validation.

  • Steps you will do
  • Start visual studio and create a new window application. 
  • Set the following properties of form

Name = frmMain

Text = Only Numbers

FormBoarderStyle  = FixedSingle

Maximize = False

 

  • Place a label control on the form and set the following properties of label.

Name = lbl

Text = Enter only numeric value in the text field below.

  • Now drop a text box control and set the following properties.

Name = tbNumbers

Text = “”

 

  • Open the code window and write the following code in the keypress event of the text box:

Dim Numbers As String = ".1234567890" + Microsoft.VisualBasic.ChrW(13) + Microsoft.VisualBasic.ChrW(8)

 

  If InStr(Numbers, e.KeyChar) = 0 Then

 

            MessageBox.Show("Input numbers only", "Numbers Only", _

            MessageBoxButtons.OK, MessageBoxIcon.Information)

 

            e.Handled = True

 

        End If

 

        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then

 

            e.Handled = True

 

        End If

Article Comments
pls send the code in asp.net only numeric value accepted in text box

Posted on 1/15/2007 12:40:01 AM by Manmohan

Article is nice, it served my purpose.

Posted on 4/10/2007 2:44:47 AM by srilakshmi

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to know Your Logical Disk Drives

Disclaimer - Privacy
© 2002-2017 DevASP.net