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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit Textbox input?

Status
Not open for further replies.

swk003

IS-IT--Management
Feb 10, 2004
86
GB
how do i limit entry into a textbox to either a 7 digit number or 6 digit alphanumeric string? I have tried the following code but no luck, is it a syntax error? can anyone help?

If Not (txtFileName.Text = "[0-9],[0-9],[0-9],[0-9],[0-9],[0-9],[0-9]" Or txtFileName.Text = "[0-9],[0-9],[0-9],[0-9],[0-9],[A-Z]") Then

ErrorProvider1.SetError(txtFileName, "Incorrect Number!")
txtFileName.Focus()
txtFileName.Undo()
txtFileName.Clear()
Else : ErrorProvider1.SetError(txtFileName, Nothing)
End If
 
Dim myRegExp As System.Text.RegularExpressions.Regex
If myRegExp.IsMatch(TextBox1.Text, "......") = True Then
' ok!
Else
' error here!
End If
 
Thanks Top-Giver...I was heading in the right direction, but this bit of code works perfectly:

'Dim myRegExp As System.Text.RegularExpressions.Regex
'If myRegExp.IsMatch(txtFileName.Text, "[0-9][0-9][0-9][0-9][0-9][A-Z]") = True Then
' ' ok!
' DateTimePicker1.Focus()
'Else
' ' error here!
' txtFileName.Focus()
'End If
 
Hi TopGiver

can you help with some if..else nested syntax? If the first txtFileName.Text expression is true I want to evaluate if the txtFileName.Text number has a '1' or a '2' as the 7th digit. If the number is a '2' I want to set cboEyes.SelectedIndex = 1 else leave it as it is...problem is though that cboEyes.SelectedIndex changes to 1 regardless. It must be relatively simple...just can't see it! Context:

Dim myRegExp As System.Text.RegularExpressions.Regex
If myRegExp.IsMatch(txtFileName.Text, "[0-9][0-9][0-9][0-9][0-9][0-9][1-2]") = True Then
' ok!
MsgBox("Number Accepted")
If (txtFileName.Text = "[0-9][0-9][0-9][0-9][0-9][0-9][1]") = False Then
cboEyes.SelectedIndex = 1
Else
cboEyes.SelectedIndex = 0
End If
DateTimePicker1.Focus()

Else
' error here!
txtFileName.Focus()
txtFileName.Undo()
txtFileName.Clear()

End If
 
This line:

If (txtFileName.Text = "[0-9][0-9][0-9][0-9][0-9][0-9][1]") = False

is directly comparing txtFilename.Text with "[0-9][0-9][0-9][0-9][0-9][0-9][1]"


and does not use your regex

Hope this helps.


[vampire][bat]
 
Aha...regex does the trick..thanks for your help earthandfire. Here's the code:

Dim myRegExp As System.Text.RegularExpressions.Regex
If myRegExp.IsMatch(txtFileName.Text, "[0-9][0-9][0-9][0-9][0-9][0-9][1-2 or A-Z]") = True Then
' ok!
MsgBox("Number Accepted")
If myRegExp.IsMatch(txtFileName.Text, "[0-9][0-9][0-9][0-9][0-9][0-9][1]") = False Then
cboEyes.SelectedIndex = 1
Else
cboEyes.SelectedIndex = 0
End If
DateTimePicker1.Focus()

Else
' error here!
txtFileName.Focus()
txtFileName.Undo()
txtFileName.Clear()

End If
 
....how do i limit entry into a textbox to either a 7 digit number or 6 digit alphanumeric string? ....

This is similar but a bit more concise approach:

Private Sub txtFileName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFileName.Validating
Dim strDgtsOnlyPtrn As String
Dim strDgtsAndLtrsPtrn As String
Dim objRegEx As System.Text.RegularExpressions.Regex

strDgtsOnlyPtrn = "\d{7}"
strDgtsAndLtrsPtrn = "\w{6}"

With txtFileName
e.Cancel = (Not objRegEx.IsMatch(.Text, strDgtsOnlyPtrn) Or .Text.Length <> 7)
If e.Cancel Then
e.Cancel = Not objRegEx.IsMatch(.Text, strDgtsAndLtrsPtrn) Or .Text.Length <> 6
End If
End With

objRegEx = Nothing
End Sub

vladk
 
A slight variation:

Code:
    Private Sub txtFileName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFileName.Validating

        Dim strDgtsOnlyPtrn As String = "\d{7}"
        Dim strDgtsAndLtrsPtrn As String = "\w{6}"
        Dim objRegEx As System.Text.RegularExpressions.Regex

        With txtFilename
          Select Case txtFilename.Text.Length
            Case 6
              e.Cancel = Not objRegEx.IsMatch(.Text, strDgtsAndLtrsPtrn)
            Case 7
              e.Cancel = Not objRegEx.IsMatch(.Text, strDgtsOnlyPtrn)
            Case Else
              e.Cancel = True
          End Select
        End With

        objRegEx = Nothing

    End Sub

Hope this helps.


[vampire][bat]
 
Another Alternate (using a single pattern with a single call to IsMatch):

Code:
Private Sub txtFileName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFileName.Validating

        Dim strPattern As String = "^\d{7}$|^\w{6}$"
        Dim objRegEx As System.Text.RegularExpressions.Regex

        e.Cancel = Not objRegEx.Ismatch(txtFileName.Text, strPattern)
        objRegEx = Nothing

    End Sub
 
bjd4jc, I was thinking along similar lines but decided against it because swk003's first post used an errorprovider. Using the case statement allows for a precise error message rather than "What you entered is wrong"


A couple of other points regarding the actual regex

\d{7} is 7 digits (ie the final digit can be 0..9 not just 1..2 (see earlier post by swk003)

\w{6} includes the underscore character _ , maybe [A..Za..z0..9]{6} would be better


Hope this helps.



[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top