alphanumeric validator

SIMIN

Regular
Joined
Mar 10, 2008
Messages
92
Hello,
I've been asked to validate the content of a text box to make sure it's only alphanumeric.
Now I have 2 question on this from you experts:

1. alphanumeric value needs to start with alpha or it can start with numbers also?

2. I wrote the following code to test my entry but it's somewhere wrong I cannot understand! How can I go through each character to make sure it's alphanumeric?

Visual Basic:
For MyLoop As Integer = 0 To PasswordTextBoxX.Text.Length - 1
    If PasswordTextBoxX.Text.Substring(MyLoop, 1) <= "a" Or PasswordTextBoxX.Text.Substring(MyLoop, 1) >= "z" And _
       PasswordTextBoxX.Text.Substring(MyLoop, 1) <= "A" Or PasswordTextBoxX.Text.Substring(MyLoop, 1) >= "Z" And _
       PasswordTextBoxX.Text.Substring(MyLoop, 1) <= "0" Or PasswordTextBoxX.Text.Substring(MyLoop, 1) >= "9" Then
        MsgBox("invalid")
    End If
Next
 
You could check each character using the Char.IsLetterOr Digit function, or a regular expression match could be used

Visual Basic:
        If Regex.Match(PasswordTextBoxX.Text, "^[a-zA-Z0-9]+$").Captures.Count > 0 Then
            'is alphanumeric 
        End If
 
If you want to see if a particular character is alphanumeric you can use the Char.IsLetterOrDigit().
 
Back
Top