Solved

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

Posted on 2014-02-26
6
213 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

759 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