Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restrict textbox input to numeric between 0 & 255

Status
Not open for further replies.

russgreen

Programmer
Dec 7, 2002
86
GB
I have restricted textbox input to numeric with the following code

Private Sub txtACI_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtACI.KeyPress
If Not Char.IsDigit(e.keychar) And Not Char.IsControl(e.keychar) Then
e.Handled = True
End If
End Sub

How do I further restrict the input to be between a number of 0 and 255?

Russ

Regards,
Russ

 
You cannot check the value in the keypress. It will try to validate everynumber so you will never be able to put in 255. You can use a regular expression and either a ErrorProvider control (For a windows app) or a RegularExpression Validator Control( for a web app )

I am going to take a guess and say that you are trying to match and IP address. This regular expression will match an ip address that is like :

10.55.255.255

Of course the numbers between the periods can be anything bewtween 0 and 255. Here it is

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

to use it you need to use the RegEx object.

'Your regex object
Dim oRegEx As Regex
Dim x As Boolean
'The parameters are
oRegEx = New Regex("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")

'This checks a specific text box
x = oRegEx.IsMatch(Me.txtAmount.Text)


If x = False Then
'This sets the error provider
ErrorProvider1.SetError(txtAmount, "IP must be in correct format 255.255.255.255")

Else
'This clears it for the textbox
ErrorProvider1.SetError(txtAmount, "")
End If

One last thing. You need to use the following imports statement in your code

Imports System.Text.RegularExpressions

Hope this helps. Let me know if you have questions.



DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
there is a RangeValidator control that can be used for this.
ControlToValidate="textbox1"
MinimumValue="1"
MaximumValue="255
 
This is VB.NET so:

1. use a masked textbox with mask "000"
2. add control error provider and/or write the code to validate in the "maskedtextbox_validated"

Code:
if maskedtextbox.text.trim >=0 and maskedtextbox.text.trim <=255 then
 ...
else
   'error
end if

Also a byte has value from 0 to 255, so
use a simple textbox:

Code:
try
  dim myValue as byte = textbox.text.trim
  '  continue
catch ex as exception
   msgbox "enter numeric only with value 0 to 255 !!!"
end try



-bclt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top