Solved

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

Posted on 2014-02-26
6
208 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now