Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi! So I was trying to do my assignment and it asks me to validate an alpanumeric, not knowing how to do it, so I look into some places around the internet and found the most readable code for me as pasted below. but for some reason it doesn't work, am I missing something? Or did I do something wrong?

What I have tried:

Public Class LoginForm

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Form1.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("Username and password cannot be null")
        ElseIf TextBox1.Text.Length < 6 Or TextBox1.Text.Length > 16 Then
            MsgBox("Username and password length must be between 6 and 16")
        ElseIf TextBox2.Text.Length < 6 Or TextBox2.Text.Length > 16 Then
            MsgBox("Username and password length must be between 6 and 16")
        ElseIf IsAlphaNum(TextBox1.Text) = False Then
            MsgBox("Username must be alphanumeric")
        End If
    End Sub

    Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
        Dim IsAlpha As Boolean = False
        If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
            IsAlpha = True
        Else
            IsAlpha = False
        End If
        Return IsAlpha
    End Function
End Class
Posted
Updated 13-Jan-17 12:23pm

Well, the regex should work. But ... your code doesn't care what happens if it does or doesn't work in that all you do is put up a message box if it fails and continue.
Probably, you need to add an Else clause to the click handler to do something specific when the pattern does match. But we don't know how your code is supposed to work, so I can't say what.

I'd suggest that you would gain a lot if you used the debugger to follow exactly what is going on - we can't do that for you!
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
See what you can find out.
 
Share this answer
 
Comments
Renille 14-Jan-17 2:18am    
yes, I just wanted to check wether the code is running correctly or not, if yes then I'll start to create a function of what will happened if the input is valid. I tried using breakpoint and see the value of isAlpha step by step from the line where the if start in isAlpha function. but isAlpha always returns true value no matter what I input, like "qqqqqqqq", the isAlpha should get a false value right? Or did i need to import a library? I've checked another code and it requires me to import library, but when I use this code, it doesn't requires me to import anything, so I use this one
OriginalGriff 14-Jan-17 3:48am    
No! Your isAlpha function permits upper and lower case letters, as well as numbers.
Try 12+34 and it'll give you an "false"
Renille 14-Jan-17 8:39am    
oh my, i thought that function will return true only if the string contains lower, uppercase letter and numbers in it, sorry for that misunderstanding. thank you! i've found a way to do it now
OriginalGriff 14-Jan-17 8:45am    
You're welcome!
As a suggestion, get a copy of Expresso

http://www.ultrapico.com/Expresso.htm

It's free, and it examines and generates Regular expressions. I use it a lot for test and check on regexes!
Your code can be simplified
VB
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
    Dim IsAlpha As Boolean = False
    If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
        IsAlpha = True
    Else
        IsAlpha = False
    End If
    Return IsAlpha
End Function

to
VB
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
    Dim IsAlpha As Boolean = False
    IsAlpha = System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$")
    Return IsAlpha
End Function

and to
VB
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
    Return System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$")
End Function


In case you need more complicated RegEx:
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
Comments
Renille 14-Jan-17 8:41am    
thank you for your reply!
Patrice T 14-Jan-17 9:35am    
You welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900