If Not Regex.Match(txt_Username.Text, "^[a-zA-Z0-9]*$", RegexOptions.IgnoreCase).Success Then
How can I change this line so it checks txt_Password as well as txt_Username?
Thanks
You can share a regex by making a regex object first, and then using its instance methods, like this:
Dim checker As Regex = New Regex("^[a-zA-Z0-9]*$", RegexOptions.IgnoreCase)
If Not checker.Match(txt_Username.Text).Success OrElse Not checker.Match(txt_Password.Text).Success
Another approach is to create an array of the values you want to test, then use Array.TrueForAll(). This example also makes sure that none of them are blank:
Dim values() As String = {txt_Username.Text, txt_Password.Text}
If Not Array.TrueForAll(values, Function(x) Regex.IsMatch(x, "^[a-zA-Z0-9]*$", RegexOptions.IgnoreCase)) _
OrElse Not Array.TrueForAll(values, Function(x) x.Trim.Length > 0) Then
MessageBox.Show("Invalid UserName and/or Password")
End If
Slightly overkill for just two TextBoxes, but just something to keep in your toolbox for when you need to check a series of TextBoxes.
Regex
can be applied by using its static methods but also by creating a reusable Regex
instance:
Dim validator As New Regex("^[a-zA-Z0-9]*$", RegexOptions.IgnoreCase)
If Not validator.IsMatch(txt_Username.Text) OrElse _
Not validator.IsMatch(txt_Password.Text) Then
...
End If
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.