By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

How to give email validation inside a textbox?

P: 4
I have the following program (C# VS 2005) for validating an email id entered in a textbox.
It works fine.

Namespace used -
using System.Text.RegularExpressions;


At present I have written the functionality inside textBox1_Validating event as shown below and when I click button1(my form contains button control called button1), it is giving me the perfect output.

Expand|Select|Wrap|Line Numbers
  1. private void textBox1_Validating(object sender, CancelEventArgs e)
  2.         {
  3.             System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
  4.             if (textBox1.Text.Length > 0)
  5.             {
  6.                 if (!rEMail.IsMatch(textBox1.Text))
  7.                 {
  8.                     MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  9.                     textBox1.SelectAll();
  10.                     e.Cancel = true;
  11.                 }
  12.             }
  13.         }
When I clicked button1, it gave me the required output.


But sir, I want some slight change.

1. I again dragged and dropped button2, button3 into my form.
Now if I click either button1 or button2 or button3 it is showing the massage box. This should be restricted. ie the error message should be shown only on the click of button1(ie my save button) and not on the click of other buttons.

2. after entering the mail id in the textbox and if I press the enter key, then it should show me the same error message box

3. if the textbox is empty and if I press enter key or button1(my save button)then also it should prompt me to enter an email id by showing the same message box.

Please help me telling what pieces of code I must write inside the following event handlers
1.
Expand|Select|Wrap|Line Numbers
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2.    {
  3.    }    
2.
Expand|Select|Wrap|Line Numbers
  1. private void textBox1_Validating(object sender, CancelEventArgs e)
  2.    {
  3.  
  4.    }
3.
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.    {
  3.    }
I am not sure whether all the above three event handlers are required , what all pieces of code I must write in each event handler or any additional event handlers are required(if so, what code I must write inside them ) to achieve the above mentioned functionality
Please help me.
Thanks and Regards,
-jm
Jan 21 '08 #1
Share this Question
Share on Google+
1 Reply


Shashi Sadasivan
Expert 100+
P: 1,435
Hi jm,
You dont need to paste the same code in all the event handlers (but the compiler would do it)

create a method which validates the email address, and on each even (textBox_validating, button_click, etx) call that method.

Cant get more difficult than that
Jan 21 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.