Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

validate textbox that can contain set of 3 digits separated by a space.

I have a textbox that can contain any number of space delimited strings.
Each string must be 3 digits.
The first 2 digits must be letters.  The third must be a number.

AA4 BE9 II4                   Valid
AA4BE9                          Invalid as there is no space
AA4 BEK                        Invalid as the last digit is a K not a number
AA9 PO5 WW3  QQ4    invalid as there are 2 spaces between WW3 and QQ4
ZZ3                                Valid

I'm using vb 2008 with asp.net 3.5.
Avatar of AlHal2
AlHal2
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

This seems to work.  What do people think?

If txtDatatypes.Text.Length Mod 4 = 3 Then
            For RegexCount = 0 To txtDatatypes.Text.Length - 1
                If RegexCount Mod 4 = 0 Or RegexCount Mod 4 = 1 Then
                    If Not Regex.Match(txtDatatypes.Text.Substring(RegexCount, 1), "^[A-Z][a-z]*$", RegexOptions.IgnoreCase).Success Then
                        lblErrs.Text = "The " + (RegexCount + 1).ToString + "th character must be a letter"
                        Exit Sub
                    End If
                End If
                If RegexCount Mod 4 = 2 Then
                    If Not Regex.Match(txtDatatypes.Text.Substring(RegexCount, 1), "^[0-9]*$", RegexOptions.IgnoreCase).Success Then
                        lblErrs.Text = "The " + (RegexCount + 1).ToString + "th character must be a number"
                        Exit Sub
                    End If
                End If
                If RegexCount Mod 4 = 3 Then
                    If txtDatatypes.Text.Substring(RegexCount, 1) <> " " Then
                        lblErrs.Text = "The " + (RegexCount + 1).ToString + "th character must be a space"
                        Exit Sub
                    End If
                End If
            Next
        Else
            lblErrs.Text = "Datatypes box must be a set 3 letter datatypes followed by a space"
            Exit Sub
        End If
ASKER CERTIFIED SOLUTION
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why don't you use regex to check if the string consists of two characters and a digiti, followed byzero or more sets of 'a space followed by two characters and a digit'?

From the top of my head something like:
[A-Za-z][A-Za-z][0-9](\ [A-Za-z][A-Za-z][0-9])*
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlHal2

ASKER

Thanks.