0

I am trying to use the Like operator to make sure that a String matches against a Pattern. I know that it is easier to use regex (Regular Expression), but let’s just say that I have to use the “like” operator. How should I go about it? Any help would be much appreciated.

The password textbox should at least contain one character, one number and one of these symbols #%&*.

let’s keep it simple and BTW this is how far I’ve managed to come with the email pattern.

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    If TextBox2.Text Like "*[@]*?.[a-z]*" Then
        MsgBox("OK")
    Else
        MsgBox("NO GOOD")
    End If
End Sub
Nevel
  • 27
  • 6
  • for simple email validation, I just check the string using the `Contains` function and search for an "@" and a ".". This works good enough for an initial check, then I send an email to them to validate that the email address itself is actually good, as there's a lot of things you're not accounting for (a username at an ip address is actually a valid email address for example, although not used very commonly). – user2366842 Feb 12 '15 at 14:30

3 Answers3

2

The password textbox should at least contain one character, one number and one of these symbols #%&*.

The only way to solve this with the LIKE operator is to combine multiple LIKE operations, since there's no concept of OR or lookahead/lookbehind like regex.

Here's an example:

Dim strings = {"das6723&", "das6723", "#das6723", 
               "fsdfdfs", "f74/&g3", "232323", 
               "ABC37&28", "J**1", "j87#"}

For Each s in strings
    Console.Write(s)

    If s Like "*#*" AndAlso _ 
       s Like "*[#%&*]*" AndAlso _
       s Like "*[a-zA-Z]*" Then

       Console.WriteLine(" : valid")
    Else
        Console.WriteLine(" : not valid")
    End If
Next
sloth
  • 99,095
  • 21
  • 171
  • 219
1

The password textbox should at least contain one character, one number and one of these symbols #%&*.

For your requirement you neither need regex nor the Like-operator, this is more efficient and readable as well:

Dim isValid = text.Length > 0 AndAlso
    text.Any(AddressOf Char.IsDigit) AndAlso
    text.Any(AddressOf "#%&".Contains)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-1

You should look for regex. Here is an example from MSDN for finding email adresses in VB.

Function ValidateEmail(ByVal email As String) As Boolean
    Dim emailRegex As New System.Text.RegularExpressions.Regex( 
        "^(?<user>[^@]+)@(?<host>.+)$")
    Dim emailMatch As System.Text.RegularExpressions.Match = 
       emailRegex.Match(email)
    Return emailMatch.Success
End Function

https://msdn.microsoft.com/en-us/library/vstudio/txk0hsah%28v=vs.100%29.aspx

More on regex in VB: http://support2.microsoft.com/kb/818802

25r43q
  • 613
  • 4
  • 16
  • _"I know that it is easier to use regex (Regular Expression), but let’s just say that I have to use the “like” operator."_ – Tim Schmelter Feb 12 '15 at 14:03
  • In that case, define a function that does the regex in the background... or you could check this one out: http://stackoverflow.com/questions/1331084/how-do-i-validate-email-address-formatting-with-the-net-framework – 25r43q Feb 12 '15 at 14:13
  • Didn't know `°@@@°` is a valid email address :-) – sloth Feb 12 '15 at 14:18
  • If it passes the regex it is valid ;) – 25r43q Feb 12 '15 at 14:42