Does anyone know how to properly Validate a Email in a C# Form?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I want my validator to read what the user puts into the Email textbox and make sure its a valid email. If not I want a MessageBox to show up and say, Email is not valid. Please enter valid email address.

I have some code that I been working on that I will post.


This is my validator... Disregard the beginning, it has to do with Bad words in textboxes that I was working on before.

Validator.cs



<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text.RegularExpressions;

<span style="color:Blue; namespace ContactMgr
{
<span style="color:Blue; public <span style="color:Blue; class Validator
{
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; bool ContainsBadWords(<span style="color:Blue; string temp)
{
<span style="color:Blue; bool result = <span style="color:Blue; false;
<span style="color:Blue; if (temp.Contains(<span style="color:#A31515; "poopy"))
{
result = <span style="color:Blue; true;
}
<span style="color:Blue; return result;
}


<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; bool isEmail(<span style="color:Blue; string inputEmail)
{
inputEmail = NulltoString(inputEmail);
<span style="color:Blue; string strRegex = <span style="color:#A31515; @"^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}" +
<span style="color:#A31515; @".[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+" +
<span style="color:#A31515; @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
Regex re = <span style="color:Blue; new Regex(strRegex);
<span style="color:Blue; if (re.IsMatch(inputEmail))
<span style="color:Blue; return (<span style="color:Blue; true);
<span style="color:Blue; else
<span style="color:Blue; return (<span style="color:Blue; false);
}





}
}

[/code]


Here is the form.cs page



<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;

<span style="color:Blue; namespace ContactMgr
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Green; //private class Person
<span style="color:Green; //{
<span style="color:Green; // private string Prefix;
<span style="color:Green; // private string FName;
<span style="color:Green; //private string MName;
<span style="color:Green; //private string LName;
<span style="color:Green; //private string Street;
<span style="color:Green; // private string Street2;
<span style="color:Green; //private string City;
<span style="color:Green; // private string State;
<span style="color:Green; // private string Zip;

<span style="color:Green; // private string Email;

<span style="color:Green; //public string Feedback = "";






<span style="color:Green; // public Person()
<span style="color:Green; // {
<span style="color:Green; // Prefix = "Mr.";


<span style="color:Green; // }


<span style="color:Green; // };

<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void btnSubmit_Click(<span style="color:Blue; object sender, EventArgs e)
{


Person temp;

temp = <span style="color:Blue; new Person();


temp.FName = txtFName.Text;
temp.MName = txtMName.Text;
temp.LName = txtLName.Text;
temp.Street = txtStreet.Text;
temp.Street2 = txtStreet2.Text;
temp.City = txtCity.Text;

temp.State = txtState.Text;
temp.Zip = txtZip.Text;

temp.Email = txtEmail.Text;

<span style="color:Blue; if (temp.LName == <span style="color:#A31515; "McCarthy")
{

DisplayInfo(temp);
}

<span style="color:Green; // if (temp.FName.Length > 0 && temp.LName.Length > 0)
<span style="color:Blue; if (!String.IsNullOrWhiteSpace(temp.FName) && !String.IsNullOrWhiteSpace(temp.LName))
{
DisplayInfo(temp);
}
<span style="color:Blue; else <span style="color:Blue; if (!String.IsNullOrWhiteSpace(temp.Feedback))
{
lblFeedback.Text = temp.Feedback;
}
{
{
dtpStartTime.CustomFormat = <span style="color:#A31515; "MM/dd/yyyy HH:mm:ss tt";
dtpStartTime.MaxDate = DateTime.Now.AddYears(1);

MessageBox.Show(dtpStartTime.Value.ToString());
}
<span style="color:Blue; if (txtFName.Text == <span style="color:#A31515; "")
{
MessageBox.Show(
<span style="color:#A31515; "First Name is a required field.", <span style="color:#A31515; "Entry Error");
txtFName.Focus();
}
<span style="color:Blue; if (txtMName.Text == <span style="color:#A31515; "")
{
MessageBox.Show(
<span style="color:#A31515; "Middle Name is a required field.", <span style="color:#A31515; "Entry Error");
txtMName.Focus();
}
<span style="color:Blue; if (txtLName.Text == <span style="color:#A31515; "")
{
MessageBox.Show(
<span style="color:#A31515; "Last Name is a required field.", <span style="color:#A31515; "Entry Error");
txtLName.Focus();
}

}

}


<span style="color:Blue; private <span style="color:Blue; void DisplayInfo(Person temp)
{
lblFeedback.Text = temp.Prefix + <span style="color:#A31515; " " + temp.FName + <span style="color:#A31515; " " + temp.MName + <span style="color:#A31515; " " + temp.LName + <span style="color:#A31515; " " + temp.Street + <span style="color:#A31515; " " + temp.Street2 + <span style="color:#A31515; " " + temp.City + <span style="color:#A31515; " " + temp.State + <span style="color:#A31515; " " + temp.Zip + <span style="color:#A31515; " " + temp.Email;

}

<span style="color:Blue; private <span style="color:Blue; void DisplayInfo()
{
lblFeedback.Text = <span style="color:#A31515; "unknown person, need more data";

}


}
}

[/code]


Person.cs page



<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;

<span style="color:Blue; namespace ContactMgr
{
<span style="color:Blue; public <span style="color:Blue; class Person
{
<span style="color:Blue; private <span style="color:Blue; string fName;
<span style="color:Blue; private <span style="color:Blue; string mName;
<span style="color:Blue; private <span style="color:Blue; string lName;
<span style="color:Blue; public <span style="color:Blue; string City;
<span style="color:Blue; public <span style="color:Blue; string Email;
<span style="color:Blue; public <span style="color:Blue; string Prefix;
<span style="color:Blue; public <span style="color:Blue; string State;
<span style="color:Blue; public <span style="color:Blue; string Street;
<span style="color:Blue; public <span style="color:Blue; string Street2;
<span style="color:Blue; public <span style="color:Blue; string Zip;



<span style="color:Blue; public <span style="color:Blue; string feedback = <span style="color:#A31515; "";



<span style="color:Blue; public Person()
{
}

<span style="color:Blue; public <span style="color:Blue; string FName
{
<span style="color:Blue; get
{
<span style="color:Blue; return fName;
}
<span style="color:Blue; set
{
<span style="color:Blue; if (Validator.ContainsBadWords(value))
{
feedback += <span style="color:#A31515; "ERROR: Bad Word in First Namen";
}
<span style="color:Blue; else
{
fName = value;
}
}
}

<span style="color:Blue; public <span style="color:Blue; string MName
{
<span style="color:Blue; get
{
<span style="color:Blue; return mName;
}
<span style="color:Blue; set
{
<span style="color:Blue; if (Validator.ContainsBadWords(value))
{
feedback += <span style="color:#A31515; "ERROR: Bad Word in Middle Namen";

}
<span style="color:Blue; else
{
mName = value;
}
}
}

<span style="color:Blue; public <span style="color:Blue; string LName
{
<span style="color:Blue; get
{
<span style="color:Blue; return lName;
}
<span style="color:Blue; set
{
<span style="color:Blue; if (Validator.ContainsBadWords(value))
{
feedback += <span style="color:#A31515; "ERROR: Bad Word in Last Namen";

}
<span style="color:Blue; else
{
lName = value;
}
}
}

<span style="color:Blue; public <span style="color:Blue; string Feedback
{
<span style="color:Blue; get
{
<span style="color:Blue; return feedback;
}
<span style="color:Blue; set
{
<span style="color:Blue; if (Validator.ContainsBadWords(value))
{
feedback += <span style="color:#A31515; "ERROR: Bad Word in Feedbackn";
}
<span style="color:Blue; else
feedback = value;
}
}
}

}
[/code]
<br/>
<br/>


View the full article
 
Back
Top