Web- und Software Development

ValidateEmail C#

Written By: - Jul• 01•08

Um einen eMailString auf Validität zu testen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*wird benoetigt fuer regularExpression*/
        using System.Text.RegularExpressions;
 
        /*method validateMail*/
        public bool validateMail(string Mailadress)
        {
            bool ok = true;
            bool wrong = false;
            string mailPattern = @"^[\w\._]{1,}@[\w\._]{2,}\.\w{2,4}$";
            Regex re = new Regex(mailPattern);
            Match ma = re.Match(Mailadress);
            if (ma.Success)
                return ok;
            else
                return wrong;
        }

Nicht meckern, ist meine 2. Woche C# : )

Ähnliche Beiträge

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

3 Comments

  1. Andre sagt:

    /*wird benoetigt fuer regularExpression*/
    using System.Text.RegularExpressions;

    /*method validateMail*/
    public bool validateMail(string Mailadress)
    {
    string mailPattern = @”^[\w\._]{1,}@[\w\._]{2,}\.\w{2,4}$”;
    Regex re = new Regex(mailPattern);
    Match ma = re.Match(Mailadress);
    if (ma.Success)
    return true;
    else
    return false;
    }

    Falls du die Variablen nicht weiter brauchst, reicht es auch gleich true oder false zurück zu geben.

  2. Roland sagt:

    Kliner Nitpick:

    if (ma.Success)
    return true;
    else
    return false;

    kann man auch noch vereinfachen zu

    return ma.Success;