How do I ensure that the user of my web form application has entered an email address? I have seen examples using regex and the EmailAddress(), but how can I implement one or the other in the if else statement below?
if (emailTextBox.Text == "" || emailTextBox.Text.Length > 100)
{
emailErrorString = "Email: Enter email address. No more than 100 characters.\n\n";
emailString = null;
errorMessage = true;
}
else
{
emailString = emailTextBox.Text;
emailErrorString = null;
}
I tried the following code and it came back true even when I entered an invalid email address "jj@jj. I did not enter ".com, or ,net, or anything like that:
if (emailTextBox.Text == "" || emailTextBox.Text.Length > 100 ||
IsValid(emailTextBox.Text).Equals(false))
{
emailErrorString = "Email: Enter a valid email address. No more than 100
characters.\n\n"; emailString = null; errorMessage = true;
}
else
{
emailString = emailTextBox.Text; emailErrorString = null;
}