I need to restrict the input in a text box to '0 to 9' max 10 digits
or 'NA' max 2 char.
allowing for the backspace.
Can anyone suggest the best method.
If regex is suggested I would need the formula.
I need to restrict the input in a text box to '0 to 9' max 10 digits
or 'NA' max 2 char.
allowing for the backspace.
Can anyone suggest the best method.
If regex is suggested I would need the formula.
Best method to restrict input
michael olson
Darren Tao
You can validate input in validating event
with regex:
Regex.IsMatch(
<string>, "^\d{0,10}$") 'this match a string with 0-10 digitsRegex.IsMatch(<string>, ^[a-zA-z]{0,2}$") 'this match a string with 0-2 chars beetween a and z case insensitive...
Raguvind
Good point Lepaca.
I personally would try to avoid mixing 'N/A'
with numbers. Perhaps a 'n/a' checkbox to
the side.
But, hey, what do I know
Avi29
I put this in a function which is called from the keypress event:
keyInt = (int)ch; if ((keyInt < 48 || keyInt > 57) && keyInt != 46 && keyInt != 8 && keyInt != 78 && keyInt != 65){
return false;}
else{
return true;}
I seems to work OK but I do have to rely on the operators.
Charles:
I will take a look at Billy Hollis's validation controls: http://dotnetmasters.com/
Thanks for the tip.
quame
Keypress event is not that great if you are
going to allow 'na' also.
Public Class Form1
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
' Use the 'maxlength' suggestion also
Dim chk As Boolean = IsNumeric(TextBox1.Text)
If Not TextBox1.Text.ToUpper = "NA" And Not chk Then
MsgBox("Must be numeric or 'NA'")
TextBox1.Select()
End If
End Sub
End Class
Tom H.
Just Phill
I was kind of hoping I could check for these conditions in the keypress event.
sdelmas
Hi,
I would suggest you use Billy Hollis's validation controls: http://dotnetmasters.com/
These are windows forms controls that work on a concept similar than asp.net validation.
For example once you drop the textbox validator on your form, for each textbox you can select which datatype if must contain (int, double, string, date, etc), the maximum length, whether it is required, min max values, regular expression or a custom validation method.
Try it, you'll like it.
Charles
Maclau
You asked for the "best method"... Now, I'm not saying this is the "best" but if you want your validation to occur on the KeyPress event, the following code will work well for you:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.GetUnicodeCategory(e.KeyChar) = Globalization.UnicodeCategory.Control Then
Dim testtxt As String
testtxt = Me.TextBox1.Text.Insert(Me.TextBox1.SelectionStart, e.KeyChar)
If Integer.TryParse(testtxt, Nothing) Then
If testtxt.Length > 10 Then
e.Handled = True
End If
Else
Select Case testtxt
Case "N", "n", "NA", "na", "Na", "nA"
Case Else
e.Handled = True
End Select
End If
End If
End Sub
You'll need to set ShorcutsEnabled = False on the TextBox to avoid Paste issues.
-EDIT- The Select statement could test the ToUpper() or ToLower() of "testtxt" and then you don't need as many cases, but the example allows any combination of the letters na.
Hope that helps; good luck!
Duncan Woods
in this case, isnumeric isn't a good solution, because i.e.
IsNumeric("12.3,") is true!
in validating event, regex works better...
Duckocide
change the textlength to 10 and on the keypress event, check to see if the character pressed is numeric. if not, set e.Handled = true;
private void TextBox1_keypress (object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.Iscontrol(e.KeyChar))
{
e.Handled = true;
}
}