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
- private void textBox1_Validating(object sender, CancelEventArgs e)
- {
- 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]$");
- if (textBox1.Text.Length > 0)
- {
- if (!rEMail.IsMatch(textBox1.Text))
- {
- MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- textBox1.SelectAll();
- e.Cancel = true;
- }
- }
- }
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
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- }
Expand|Select|Wrap|Line Numbers
- private void textBox1_Validating(object sender, CancelEventArgs e)
- {
- }
Expand|Select|Wrap|Line Numbers
- private void button1_Click(object sender, EventArgs e)
- {
- }
Please help me.
Thanks and Regards,
-jm