Solved

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

Posted on 2014-02-26
6
210 Views
Last Modified: 2014-02-27
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.
0
Comment
Question by:AlHal2
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:AlHal2
ID: 39888986
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
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 200 total points
ID: 39890908
It looks like it would work, but to be honest it's not very nice-looking code, at least to my eyes.

Personally, I wouldn't bother with Regex for checking a single character for being alphabetic or numeric; I'd use .IsLetter and .IsDigit instead, which can actually work directly on a specific character in a string without needing to do a .Substring. Rather than having three IFs in succession, when only one of them can be valid, I'd use either ElseIf or Select Case. Using "th" after any number is going to give poor results for numbers like 2 or 21, so I'd rewrite that. Finally, it's preferred to use & rather than + when putting strings together.
        If txtDatatypes.Text.Length Mod 4 = 3 Then
            For CharCount = 0 To txtDatatypes.Text.Length - 1
                Select Case CharCount Mod 4
                    Case 0, 1
                        If Not Char.IsLetter(txtDatatypes.Text, CharCount) Then
                            lblErrs.Text = "Character " & (CharCount + 1).ToString & " must be a letter"
                            Exit Sub
                        End If
                    Case 2
                        If Not Char.IsDigit(txtDatatypes.Text, CharCount) Then
                            lblErrs.Text = "Character " & (CharCount + 1).ToString & " must be a number"
                            Exit Sub
                        End If
                    Case 3
                        If txtDatatypes.Text.Substring(CharCount, 1) <> " " Then
                            lblErrs.Text = "Character " & (CharCount + 1).ToString & " must be a space"
                            Exit Sub
                        End If
                End Select
            Next
        Else
            lblErrs.Text = "Datatypes box must be a set 3 letter datatypes followed by a space"
            Exit Sub
        End If

Open in new window

0
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 39891054
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])*
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 200 total points
ID: 39891067
You'd need to add \Z at the end to make sure that you don't match on entries that start correctly, but have errors in later codes. But otherwise, I think Dennis's idea would work, though it wouldn't provide as much information about where a problem lies.
0
 
LVL 18

Assisted Solution

by:Dennis Aries
Dennis Aries earned 50 total points
ID: 39891075
ElrondCT, you are correct.
If you want to provide more information, you will have to do additional checks. Personally, I don't see the benefit of telling that the 49th position should be a digit (or not) since that would be of no help to the user. By retrieving the matches through Regex, you can tell in which set the error occurrs, which will be more helpful, I think.
0
 

Author Closing Comment

by:AlHal2
ID: 39891316
Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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