Solved

Check text in textbox is in a certain format?

Posted on 2009-04-06
4
169 Views
Last Modified: 2012-05-06
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
Comment
Question by:m0tSiE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 50 total points
ID: 24076447
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 350 total points
ID: 24076456
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
 
LVL 10

Assisted Solution

by:kdwood
kdwood earned 100 total points
ID: 24076540
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
 

Author Closing Comment

by:m0tSiE
ID: 31566973
Cheers.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question