0
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace read_test
{
    class StringCheck
    {
        static void Main(String [] args)
        {
            string email = "email@email.com";

        string regEx = @"\w+\.?\w+\@{1}\w+\.{1}\w+";

        Match aMatch;
        aMatch = Regex.Match(email, regEx);

        if (aMatch.Success)
            Console.WriteLine("Successfull.");
        else
            Console.WriteLine("Not Successfull");

        Console.Read();

    }
}

}

tried writing a regular expression to check if the email id entered is valid. Is this right? Or is there a better way

generic2709
  • 31
  • 10
  • `{1}` qualifiers are redundant. You allow only one dot in login, which is wrong. TLD should be at least 2 symbols. [Here is the RFC-compliant Regex](http://ex-parrot.com/~pdw/Mail-RFC822-Address.html), which shows you that there are plenty of requirements. And after all, it's better to listen to those guys, who said that you don't need the Regex. – Smileek Oct 24 '14 at 11:20

1 Answers1

3

try this, you dont really need a regex

bool IsValidEmail(string email)
{
    try {
        var addr = new System.Net.Mail.MailAddress(email);
        return true;
    }
    catch {
        return false;
    }
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • Probably the best and easiest way to proof the format. – C4d Oct 24 '14 at 11:26
  • Thank you for this, I learnt something new. I was told to write a regular expression, that is why I needed to know if the code is correct? and if there is a better way to use regular expression. – generic2709 Oct 24 '14 at 11:26