-1

I have this function of which works fine however is there and easier way to complete the validation check using the mail address class, and would it be more fitting. Thanks in advance.

        TextBox tb = new TextBox();
        tb.KeyDown += new KeyEventHandler(txtEmail_KeyDown);

        string strRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))";

        Regex re = new Regex(strRegex); // New regex Object  created 

        // Run Checks after the enter is pressed.
        if (e.KeyCode == (Keys.Enter))
        {
            // checks for is match, if empty and length 
            if (!re.IsMatch(txtEmail.Text) || (txtEmail.Text.Equals("")) || txtEmail.Text.Length > 100)
            {
                // display messagebox with error
                MessageBox.Show("Email not correct format!!!! ");
            }
            else
            {
                MessageBox.Show("Email Format is correct");
            }
        }

    }

2 Answers2

2

you can validate with the EmailAddressAttribute class pretty easily like this in c#

public bool ValidateEmail(string EmailToVerify)
{
  if (new EmailAddressAttribute().IsValid(EmailToVerify))
        return true;
  else 
        return false;
}

but to use this you need to add this using at the top of your c# code page

using System.ComponentModel.DataAnnotations;

the only downside to this is that EmailAdressAttribute is not so permisive so it depends on what you want to restrict and permit

And if you need it here is the link the the msdn doc about this class : https://msdn.microsoft.com/fr-fr/library/system.componentmodel.dataannotations.emailaddressattribute(v=vs.110).aspx

Louis-Roch Tessier
  • 822
  • 1
  • 13
  • 25
0

No, it is not stable. Since any regular expression of itself represents a finite state machine, it can, in special cases, get into an infinite loop that grafts to the server's DDOS attack.
Just use MailAddress class for validation.

UPDATE 1
After testing MailAddress class and new EmailAddressAttribute().IsValid("MAIL_TEXT_HERE") I came to conclusion that EmailAddressAttribute's Validation is working better.
You can implement it in this way, let's say that you have TextBox and Button for submit. Just add this Click event handler to buttons Click Event:

private void button1_Click(object sender, EventArgs e)
{
    if(!new EmailAddressAttribute().IsValid(textBox1.Text))
    {
        MessageBox.Show("Email is not valid");
    }
    else
    {
        MessageBox.Show("Email is valid");
    }
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • cheers, to write a validation function should i use the mailAddress class or store all possibilties in a collection ? – whatdoyouNeedFromMe May 16 '17 at 14:51
  • @whatdoyouNeedFromMe just use MailAddress class – Samvel Petrosov May 16 '17 at 14:54
  • How would you implement using a user input from a text box on a winform – whatdoyouNeedFromMe May 16 '17 at 15:27
  • 1
    don`t forget about the using you need to add like i said in my answer – Louis-Roch Tessier May 16 '17 at 17:20
  • 1
    @Louis-RochTessier yeah, of course , just VS2017 offers to add it automatically, so I did not pay attention to this when I was writting – Samvel Petrosov May 16 '17 at 17:22
  • 1
    If your answer addresses only stability and a particular regex engine that truly implements a finite state machine, then perhaps you're correct. If at the the same time you also address convenience, consistency, previously-proven methods, etc., then your answer is also probably the best (i.e. no need to reinvent the wheel and mess with finding the "perfect" pattern... which itself is debatable). However, the .Net regex implementation is not strictly a FSM. It is also possible to produce a multi-pattern solution, possibly contained in a time-out block, that could be considered "stable". – C Perkins May 18 '17 at 20:34
  • @CPerkins yes, I do agree with you, but it all brings with it additional checkings. – Samvel Petrosov May 19 '17 at 05:15