Monday, January 18, 2010

Number Validation in Vb 6.0

How to validate number with IsNumeric Function?


Input from user must be validated before it is accepted and stored into the database table. For validating number visual basic provides 'IsNumeric' Function that takes the string as input parameter. The return value of this function is a boolean value either True or False.

Often the text box input values are checked for validation. If you have a text box named txtAmount for input of currency amount then you can validate it in the text box's Validate event.

I will explain number validation in VB 6.0 with an example. Create a standard EXE project in Visual Basic IDE. Put a text box and set its name property to txtAmount. Put a command button and set its name property to cmdValidate. In the Validate Event of the text box write the following code.


Private Sub txtAmount_Validate(Cancel As Boolean)
If Not IsNumeric(txtAmount.Text) Then
Cancel = True
MsgBox "Enter valid Amount", vbExclamation
txtAmount.Text = ""
End If
End Sub

In the command buttons click event write the following code,

Private Sub cmdValidate_Click()
MsgBox "Valid amount"
End Sub

The output for different inputs are as under,