Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-26
6
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 800 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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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