Solved

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

Posted on 2014-02-26
6
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

10 Experts available now in Live!

Get 1:1 Help Now