Ultimately what you can do (as per jflood.net's comment) is to separate your concerns. So currently you have 2 main concerns (you could probably even break it down further but we should avoid over engineering as well) - Presenting data to the user and validating the users' input.
Your ASP.NET WebForm code handles concern #1. In order to separate the validation concern out you should create a new validation class that handles the validation logic. Something like
public class Validator
{
public bool ValidateEmail(string email)
{
bool valid = false;
//Run RegEx validation and set the valid flag
return valid;
}
}
Now in your page load you can utiliize this new class
protected void Page_Load(object sender, EventArgs e)
{
Validator validator = new Validator();
string email = Request.Form["EmailInput"];
bool isEmailValid = validator.ValidateEmail(email);
if (isEmailValid)
Response.Write("Email is valid");
}
Note this is somewhat pseudo code since this is for homework, I didn't want to type up the full solution.
Now you have separated your validation concern into its own class. This makes it easier to understand and easier to unit test.
You will now want to write unit tests for the Validator class, specifically in this case for the ValidateEmail.
Here is an example
[Test]
public void ValidateGoodEmail()
{
Validator validator = new Validator();
bool shouldBeTrue = validator.ValidateEmail("[email protected]");
Assert.AreEqual(true, shouldBeTrue);
}
[Test]
public void ValidateBadEmail()
{
Validator validator = new Validator();
bool shouldBeFalse = validator.ValidateEmail("[email protected]");
Assert.AreEqual(false, shouldBeFalse);
}
Remember in unit testing you are just testing the different logical flows. You do not have to test ever input, so here I have tested both a false validation with a given bad email address and a true validation with a good email address.
Hope this helps