No matter what I enter my data is always true. Im taking the data from a text box that the user can enter there data in. What am I doing wrong? Utility.ValidName and Utility.Email return true or false.
Check
string username = null;
string email = null;
username = textBox1.Text;
email = textBox2.Text;
bool vusernam = false;
bool vemail = false;
vusernam = Utility.ValidName ( username );
vemail = Utility.ValidEmail ( email );
if ( vusernam == true && vemail == true )
{
Utility.WelcomeMessage ( string.Format ( "Hello {0}\nEMail: {1}\nWelcome!" , username , email ) );
secondForm.Show ( );
this.Hide ( );
}
else
{
MessageBox.Show ( "Please Enter a Username and Email Adress!" );
}
Valid Username and Email
public static bool ValidEmail ( string email )
{
string strTest = email;
Regex pattern = new Regex ( @"(?<name>\S+)@(?<domain>\S+)" );
Match match = pattern.Match ( strTest );
if ( String.IsNullOrEmpty ( email ) || !match.Success )
{
Console.Write ( "\nInvalid Email!" );
}
return true;
}
public static bool ValidName ( string name )
{
string strTest = name;
if ( String.IsNullOrEmpty ( name ) )
{
Console.Write ( "\nInvalid Name!" );
}
return true;
}
You always return true, no matter what.
Add a return false after your Console.Write. Simply outputting an error message doesn’t cause your validation to fail.
Side notes:
can be reduced to simply
and
string strTest = name;appears to serve no purpose at all.