C# .NET - error validator provider control - Asked By mostafa hamdy on 05-Oct-10 05:06 AM

Hello all
I have some windows form which contains many text boxes and some of them is mandator and some are conditional mandator , means if the user enter some text in it but this text is not true the error providater flashes and alert the user
but there's some problem with this form, if I entered some incorrect data in some text box the error provider appears to the user and if I again entered the true value the error provider still appears , my code is as the following:


Match
Email = Regex.Match(tbEmail.Text.Trim(),@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"); //@"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|[a-zA-Z]{2})*$");

Match FirstName = Regex.Match(tbFirstName.Text.Trim(),"^[a-zA-Z]|[0-9]|[a-zA-Z0-9]$");

Match LastName = Regex.Match(tbLastname.Text.Trim(), "^[a-zA-Z]|[0-9]|[a-zA-Z0-9]$");

Match UserName = Regex.Match(tbUserName.Text.Trim(), "^[a-zA-Z]|[0-9]|[a-zA-Z0-9]$");

//validate the first name if it has a value

if (!string.IsNullOrEmpty(tbFirstName.Text.Trim()))

{

if (!FirstName.Success)

{

epValidator.SetError(tbFirstName, "you have entered invalid value for first name");

//MessageBox.Show("you have entered invalid value for first name");

tbFirstName.Focus();

Result = false;

}

}

if (!string.IsNullOrEmpty(tbLastname.Text.Trim()))

{

if (!LastName.Success)

{

//MessageBox.Show("you have entered invalid value for last name");

epValidator.SetError(tbLastname, "you have entered invalid value for last name");

tbLastname.Focus();

Result = false;

}

}

if (string.IsNullOrEmpty(tbUserName.Text.Trim()))

{

//MessageBox.Show("user name can not be empty");

epValidator.SetError(tbUserName, "user name can not be empty");

tbUserName.Focus();

Result = false;

}

if (!UserName.Success)

{

//MessageBox.Show("you have entered invalid value for user name");

epValidator.SetError(tbUserName, "you have entered invalid value for user name");

tbUserName.Focus();

Result = false;

}

if (string.IsNullOrEmpty(tbEmail.Text.Trim()))

{

//MessageBox.Show("email can not be empty");

epValidator.SetError(tbEmail, "email can not be empty");

tbEmail.Focus();

Result = false;

}

if (!Email.Success)

{

//MessageBox.Show("you have entered invalid value for email");

epValidator.SetError(tbEmail, "you have entered invalid value for email");

tbEmail.Focus();

Result = false;

}

if (string.IsNullOrEmpty(tbLicenseValidation.Text.Trim()))

{

//MessageBox.Show("license key can not be empty");

epValidator.SetError(tbLicenseValidation, "license key can not be empty");

tbLicenseValidation.Focus();

Result = false;

}
please if any body get the problem and can help me please send me or tell me about some url may help me in solving it
regards
Mostafa

Vasanthakumar D replied to mostafa hamdy on 05-Oct-10 12:25 PM
Hi,

you have not handled the else part of validation....

sample...

if (!string.IsNullOrEmpty(tbFirstName.Text.Trim()))

{

if (!FirstName.Success)

{

epValidator.SetError(tbFirstName, "you have entered invalid value for first name");

//MessageBox.Show("you have entered invalid value for first name");

tbFirstName.Focus();

Result = false;

}

else

{

   epValidator.SetError(tbFirstName, ""); //remove the error here

}

}