Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Check text in textbox is in a certain format?

Hi,

I currently have a textbox that allows users to enter text into as normal, however I want to make sure that the text entered is in the following format.

XX000000X

X = Letter
0 = Number

Is there a way I can check this on a button press? So if the text is in that format, perform the action, else display a popup box asking the user to enter the text in the correct format.

Many thanks,

Paul.
0
m0tSiE
Asked:
m0tSiE
3 Solutions
 
DhaestCommented:
you can use regular expressions for that
http://www.vb-helper.com/howto_net_regular_expression.html

Private Sub txtTestExp_TextChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    txtTestExp.TextChanged
    
    Dim reg_exp As New Regex("^[a-z]{2}[1-9]{6}[a-z]{1}$"")
    If reg_exp.IsMatch(txtTestExp.Text) Then
        txtTestExp.BackColor = Color.White
    Else
        txtTestExp.BackColor = Color.Yellow
    End If
End Sub

Open in new window

0
 
kdwoodCommented:
He can also do this, if he doesn't want to use regular expressions:

Assuming you have the max length property of your textbox field set to 9:


 Dim myTBLen As String = Me.YouTextBox.Text.Length
 
        If myTBLen < 2 Or myTBLen >= 9 Then
 
            If Char.IsLetter(e.KeyChar) = False Then
 
                MsgBox("You must enter a letter in this position", MsgBoxStyle.Critical)
 
                e.Handled = True  ' Cancel the keypress event
 
            End If
 
        ElseIf myTBLen > 2 And myTBLen < 9 Then
 
            If Char.IsNumber(e.KeyChar) = False Then
 
                MsgBox("You must enter a Number in this position", MsgBoxStyle.Critical)
 
                e.Handled = True  ' Cancel the keypress event
 
            End If
 
        End If

Open in new window

0
 
m0tSiEAuthor Commented:
Cheers.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now