Using LINQ to Validate a String

Page 1 of 1

3 Replies - 3672 Views - Last Post: 09 November 2014 - 05:28 AM

#1 andrewsw

• blow up my boots

Reputation: 6444
• Posts: 26,064
• Joined: 12-December 12

Using LINQ to Validate a String

Posted 08 November 2014 - 04:35 PM

Checking a string for required combinations of letters and numbers (or other characters) can be achieved by iterating each character in a loop, or maybe using regex. Using LINQ is an alternative that can be quite expressive.

The first step is most often to confirm the required string length:
```Dim sTest As String = "abc12"
Dim passed As Boolean = sTest.Length = 5
```

Suppose the requirement is for three letters followed by two numbers:
```If passed Then
passed = sTest.Where(
Function(x, ind)
Return If(ind < 3, Char.IsLetter(x), Char.IsNumber(x))
End Function).Count() = 5
End If
MessageBox.Show(passed)
```

This uses the character-index (ind). The If() function can be nested for more detailed requirements.

_HAWK_ demonstrates using Select..Case while iterating the characters here to achieve this.

If the requirement is for three letters and two numbers, in any order:
```If passed Then
Dim letters As Integer = sTest.Count(Function(x) Char.IsLetter(x))
Dim numbers As Integer = sTest.Count(Function(x) Char.IsNumber(x))

passed = (letters = 3) And (numbers = 2)
End If
```

(Don't forget to check the Length first.)

For three lower-case letters and two numbers, in any order:
```If passed Then
Dim lowerLetters As Integer = sTest.Count(Function(x) Char.IsLetter(x) AndAlso Char.IsLower(x))
Dim numbers As Integer = sTest.Count(Function(x) Char.IsNumber(x))

passed = (lowerLetters = 3) And (numbers = 2)
End If
```

There are other Char functions that can be employed:
IsLetterOrDigit, IsPunctuation, IsSymbol, and more.

We can also check for specific, non-alphanumeric characters:
```Dim acceptable As String = "[]()!'\$_-"
passed = sTest.Count(Function(x) acceptable.Contains(x)) = 2
```

This post has been edited by andrewsw: 08 November 2014 - 06:07 PM

Is This A Good Question/Topic? 0

Replies To: Using LINQ to Validate a String

#2 andrewsw

• blow up my boots

Reputation: 6444
• Posts: 26,064
• Joined: 12-December 12

Re: Using LINQ to Validate a String

Posted 08 November 2014 - 07:15 PM

I am not a fan of MaskedTextBoxes but, out of curiosity, I explored the possibility of borrowing its Mask to validate a string:
```        Dim mtb As MaskedTextBox = New MaskedTextBox()
Dim sTest As String = "abbc12"
mtb.Mask = "LLL00"
AddHandler mtb.MaskInputRejected, Sub(obj As Object, mea As MaskInputRejectedEventArgs)
MessageBox.Show(mea.RejectionHint.ToString())
'e.g. "DigitExpected"
End Sub
mtb.Text = sTest
```

I suppose it is interesting that this is possible, but I doubt that it offers any advantage over the approaches already suggested.

This post has been edited by andrewsw: 08 November 2014 - 07:17 PM

Was This Post Helpful? 0

#3 AdamSpeight2008

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

Re: Using LINQ to Validate a String

Posted 08 November 2014 - 07:46 PM

There is the much forgotten Like operator.
```    Dim text ="ABC00"
Dim valid = text Like "[A-Za-z][A-Za-z][A-Za-z]##"

```

Was This Post Helpful? 1

#4 andrewsw

• blow up my boots

Reputation: 6444
• Posts: 26,064
• Joined: 12-December 12

Re: Using LINQ to Validate a String

Posted 09 November 2014 - 05:28 AM

I'd forgotten about that

Like replaces my first LINQ example and and my whimsical exploration of the MaskedTextBox. The only remaining interesting aspect of an MTB is the RejectionHint:

Spoiler

This post has been edited by andrewsw: 09 November 2014 - 05:56 AM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }