Textbox Validation Problem, NEED HELP!!!!

Stefan

New member
Joined
Apr 25, 2009
Messages
1
Programming Experience
1-3
Here is my code:

If (txtOrderNo.Text.Chars(0) = "A" Or "B" Or "C" Or "X") And (txtOrderNo.Text.Chars(1) = "0" Or "1" Or "2" Or "3") Then

ListBox1.Items.Add(txtOrderNo.Text)

Else
MsgBox("error")

What I want to do is, the input that the user puts in, must be up to 7 chars long, but the first character must be A, B, C or X and the second character must be 0, 1, 2 or 3 then the rest are numeric.

Oh and, once the input is correct, it adds the order number into a list box.

Can anyone help?

Thanks!!!!
 
You don't do this:
VB.NET:
txtOrderNo.Text.Chars(0) = "A" Or "B"
You do this:
VB.NET:
txtOrderNo.Text.Chars(0) = "A" Or txtOrderNo.Text.Chars(0) = "B"
Also, you'd be much better off using a Select Case statement:
VB.NET:
Select Case txtOrderNo.Text(0)
    Case "A"c, "B"c, "C"c, "X"c
        'First character OK.
End Select
You then nest a second Select Case inside that for the second character.

That said, you need to validate the length too. The very first thing to do would be to test that it's not longer than 7 characters because that's the easiest. Next you must test whether it's at least 1 character long before testing the first character, otherwise an exception will be thrown when you try to get the first character. You have to test the length again before testing the second character. Once you get through all that and the data is still valid, you should then test that it's at least 3 characters long. If it is then use Substring to get everything after the second character and then Integer.TryParse to test that it's numeric.
 

Latest posts

Back
Top