Text Validation

  • Thread starter Thread starter David
  • Start date Start date
D

David

Can anyone point me in the right direction. I am tring to ensure that the
user input in a textbox is a Number.

Any help would be great
Thanks
God Bless
 
David,

One option is to perform input validation in the textbox’s Validating event.

When the user changes the focus from a textbox (or other control) the
textbox’s Validating event occurs.

In the Validating event you can write code to validate the data in the
textbox. If the user has entered invalid data you can inform the user and
cancel the Validating event.

Canceling the textbox’s Validating event prevents the focus from leaving the
textbox. This forces the user to enter good data into the textbox in order to
continue.

For example, in the txtLength textbox’s Validating event you can test for
invalid input like this:

Private Sub txtLength_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles txtLength.Validating

'If the text in the txtLength textbox is not numeric
If Not IsNumeric(txtLength.Text) Then
'Inform the user that length must be a number
MsgBox("Length must be a number", _
MsgBoxStyle.Exclamation, "Input Error")
'Cancel the Validating event
'to prevent the focus from changing
e.Cancel = True
End If

End Sub

Another option is to perform input validation in a textbox’s Validating
event. But instead of using a message box to display error conditions, use an
ErrorProvider control.

An ErrorProvider control provides an unobtrusive graphical display that a
control contains invalid data.

Kerry Moorman
 
Insert a MaskedTextBox from the VB.NET toolbox. Then go to the property
window & select the mask property. Modify as needed.

William Cruz
 
Insert a MaskedTextBox from the VB.NET toolbox. Then go to the property
window & select the mask property. Modify as needed.
Just an addition, this is from version VB2005

Cor
 
David,

In addition of the others, you first have to define what is a Number.

The IsNumeric by instance is very strict in that and except every thing that
can be a numeric value in the culture setting of the client.

Is it meant by instance that it have to be only characters between Chr(0)
and Chr(9), than you cannot use IsNumeric, however maybe is even a dot
allowed in your number.

Just as addition.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top