3

I have a windows form application were i named a textbox called textbox1, I want that the user only can input something like an e-mail like this: sample@email.com. not some random text or random numbers.

Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35
  • 3
    I suppose that you really need to read this http://stackoverflow.com/questions/5342375/c-sharp-regex-email-validation – Steve Oct 22 '15 at 10:35

1 Answers1

4

Use the regex as below :)

private void txtEmail_Leave(object sender, EventArgs e)    
 {    
    Regex mRegxExpression;    
    if (txtEmail.Text.Trim() != string.Empty)    
     {
       mRegxExpression = new Regex(@"^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$");

        if (!mRegxExpression.IsMatch(txtEmail.Text.Trim()))    
         {    
           MessageBox.Show("E-mail address format is not correct.", "MojoCRM", MessageBoxButtons.OK, MessageBoxIcon.Error);    
           txtEmail.Focus();    
         }    
     }    
 }
Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35