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# : )







/*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.
Kliner Nitpick:
if (ma.Success)
return true;
else
return false;
kann man auch noch vereinfachen zu
return ma.Success;
Danke Dir : )