Visual Basic Validation

Associate
Joined
7 Aug 2008
Posts
302
Alryt lads.

Pretty simple I think. I want to validate a text box in Visual Basic. I.E. for the 'Title' text box I want it so it only allows particular data, e.g. "Mr", "Mrs", "Ms" etc to be entered into that field.

I know I could just use a combo box but Id rather try and do it this way around.

Chears.
 
I havent used vb for a long time so the syntax is lacking (nonexistant :P), but something like this:

if(title.text = "Mr" or "Mrs" or "Ms")//add whatever other validation you need
{
//submit or whatever
}

else
{
//error message
}
 
I havent used vb for a long time so the syntax is lacking (nonexistant :P), but something like this:

if(title.text = "Mr" or "Mrs" or "Ms")//add whatever other validation you need
{
//submit or whatever
}

else
{
//error message
}

I did try something similar too that and I just tried that syntax then. Still getting Type Mismatch error. :/

Weird.
 
It sounds like you're trying to bash a square peg into a round hole really with this.
This is the exact thing a combobox is meant for.

What you could look at if you really want people to type something in than select from a list is to use an incremental search to pre-populate the valid values as they type.

For instance if your valid values are: Dr, Mr, Mrs, Ms then typing M as the first letter will eliminate Dr and so on.
I'm sure there are some examples of this on the web if you search.
 
^ agree, but if you really want to do it this way:
Code:
  t = TextBox1.Text

        If t = "Mr" Then
            MsgBox("Ok")
        ElseIf t = "Mrs" Then
            MsgBox("Ok")
        ElseIf t = "Ms" Then
            MsgBox("Ok")
        Else
            MsgBox("Not ok")
        End If

This doesnt work very well as you will need to double up each if statement depending on lower/upper case.
 
Something like this could work. Not done much programming for ages so perhaps not the most efficient method.

Code:
Select Case (title.Text)
     Case "Mr"
          'Validation passed
     Case "Mrs"
          'Validation passed
     Case "Ms"
          'Validation passed
     Case Else
          'Validation failed
End Select

Then obviously enter whatever code you want to use when the user types something that is valid or not valid under each case. Perhaps add all that to an event handler for when the user clicks away from the textbox.
 
Back
Top Bottom