How to give email validation inside a textbox in C# windows application(VS 2005)?

Discussion in 'Software' started by Vayalumkal, Jan 21, 2008.

  1. Vayalumkal

    Vayalumkal

    Joined:
    Jan 21, 2008
    Messages:
    1
    Hi Sir,

    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.

    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;
    }
    }
    }
    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. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    }

    2. private void textBox1_Validating(object sender, CancelEventArgs e)
    {

    }

    3. private void button1_Click(object sender, EventArgs e)
    {
    }

    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
    Vayalumkal, Jan 21, 2008
    #1
    1. Advertising

  2. Vayalumkal

    enirosh

    Joined:
    Oct 14, 2008
    Messages:
    1
    you can set the causesValidation propoty into false in button 2 and 3
    then they will not active for any kind of validation....
    enirosh, Oct 14, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lib
    Replies:
    0
    Views:
    475
  2. lib
    Replies:
    1
    Views:
    512
  3. Jimmy
    Replies:
    3
    Views:
    1,719
    Render Me
    Sep 12, 2005
  4. susan_1516

    validating textbox inside datagrid

    susan_1516, Nov 9, 2008, in forum: Software
    Replies:
    0
    Views:
    1,195
    susan_1516
    Nov 9, 2008
  5. write2atif
    Replies:
    0
    Views:
    4,263
    write2atif
    Apr 30, 2009
Loading...

Share This Page