【发布时间】:2021-12-28 12:25:33
【问题描述】:
我正在构建一个电子邮件验证程序。
如何检查我的电子邮件是否在 @ 之前有 3 个字母,在“.”之后是否有 2 个字母我试过else if,但没用。
string rightPassword = "red@gmx.to";
string enter = Console.ReadLine();
string red = rightPassword.Substring(0, 3);
string to = rightPassword.Substring(8);
int lengthRed = red.Length;
int lengthTo = to.Length;
do
{
Console.Write("Write the E-Mail: ");
enter = Console.ReadLine();
if (rightPassword == enter)
{
Console.WriteLine("Right Password");
}
else if (lengthRed < 3 ) // I have to check if it has 3 letters before the @//
{
Console.WriteLine("You need 3 letters before the @")
}
else if (lengthTo < 2)
{
Console.WriteLine("You need 2 letter after the ".")
}
} while (enter != "enter");
【问题讨论】:
-
你给
Console.ReadLine();打了两次电话。在第二个之后,您不再计算red、to、lengthRed和lengthTo。这些计算应该在 ReadLine 之后的循环内。第一个 Readline 是多余的。 -
很少有正则表达式验证是电子邮件地址的正确工具。如果您需要有效电子邮件地址,请向他们发送验证电子邮件。如果他们可以完成电子邮件调用的验证,则该地址是有效的。如果他们不能,那么谁在乎它是否匹配潜在有效的电子邮件地址。
标签: c# validation if-statement