Regular expression in windows form?

dwyane123

Member
Joined
Oct 14, 2008
Messages
23
Programming Experience
Beginner
Hi, can any one help?

I have checked and realise there is no regular expression validator in windows application. How do we validate a text box to make sure that it will contain 10 characters(digits, letters doesnt matter). Just to generate to check that there are 10 letters. If it is not 10 letters, error will pop out when input is triggered.

Thanks alot
 
Hi there. You didn't look hard enought ;)

VB.NET:
        Dim myRegEx As New System.Text.RegularExpressions.Regex("^?{10}$")
        If myRegEx.IsMatch(txtBox.text) Then
            ' Is ok
        Else
            ' Is not ok
        End If

I haven't tried this but thats the general idea of it.
 
You can also go with
VB.NET:
 Private Sub txtBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtBox.Validating
        If txtBox.Text.Trim.Length < 10 Then
            e.Cancel = True

        End If
    End Sub

However I do see now that you were specifically requesting a regular expression in which hairymike's is what you want.
 
Back
Top