herobrinebrother zoro herobrinebrother zoro - 1 month ago 15
Vb.net Question

vb.net application regular expression error

I am making a application in vb.net and on almost first release but when testing I found an expression error. I have set everything correctly in code so this should not be a problem and I imported it too but I still get the same error. Here is my code area that has error. All other information of my code is not needed.

My imports:

Imports MySql.Data.MySqlClient
Imports System.Text.RegularExpressions


The code where the error is. Remember the error is regular expression:

Private Sub ButtonNUS_Click(sender As Object, e As EventArgs) Handles ButtonNUS.Click
If TextBoxNUsern.Text = "" Or TextBoxNEmail.Text = "" Or TextBoxNPass.Text = "" Or TextBoxNPhone.Text = "" Then
MessageBox.Show("Please don't leave empty areas.")
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNUsern.Text, "^[A-Za-z0-9]+$") Then
MessageBox.Show("Empty spaces or special char is not allowed in username.")
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNPass.Text, TextBoxNRPass.Text, "^[A-Za-z0-9.]+$") Then
MessageBox.Show("Empty spaces is not allowed in password.")
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNEmail.Text, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*") Then
MessageBox.Show("Email must be valid.")
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNPhone.Text, "^\+[0-9()-+]+$") Then
MessageBox.Show("Phone number must be valid.")
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNCity.Text, "^[A-Za-z ]+$") Then
MessageBox.Show("City must be valid. OPTIONAL")
ElseIf TextBoxNPass.Text <> TextBoxNRPass.Text Then
MessageBox.Show("Password dosen't match. Please try again.")
Else


The error message is:


An unhandled exception of type 'System.InvalidCastException' occurred
in Microsoft.VisualBasic.dll

Additional information: Conversion from string "^[A-Za-z0-9.]+$" to
type 'Integer' is not valid.


It says its a integer. I think but its set to string i am sure lets say this line

System.Text.RegularExpressions.Regex.IsMatch(TextBoxNUsern.Text, "^[A-Za-z0-9]+$")


Its a string because i am checking it in textboxnusern.text right? So, what's the problem here?

Answer Source

You're calling IsMatch method with false paramters

...
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNPass.Text, TextBoxNRPass.Text, "^[A-Za-z0-9.]+$")
...

but this expects the following parameters (from MSDN)

Public Shared Function IsMatch ( _
    input As String, _
    pattern As String, _
    options As RegexOptions _
) As Boolean