gpt4 book ai didi

c# - 使用 C# 进行输入验证和异常

转载 作者:行者123 更新时间:2023-12-02 20:21:34 25 4
gpt4 key购买 nike

我正在尝试验证输入并处理该程序中的异常。验证以下内容:txtNAME 中的字符串不应留空,txtTELEPHONE 中的电话号码应至少为 10 位数字,txtEMAIL 应采用电子邮件格式,并带有“@”和“.”。如何验证这些输入并在输入错误的情况下处理异常?

public partial class Form1 : Form
{
static int maxCount = 10;

int[] employeeID = new int[maxCount];
string[] employeeName = new string[maxCount];
string[] jobTitle = new string[maxCount];
string[] address = new string[maxCount];
int[] telephoneNumber = new int[maxCount];
string[] email = new string[maxCount];

int addCount = 0;

string fn = "employees.xml";

int currentRec;

private void btnADD_Click(object sender, EventArgs e)
{
employeeID[addCount] = Convert.ToInt32(txtEI.Text);
employeeName[addCount] = txtNAME.Text;
jobTitle[addCount] = txtJOB.Text;
address[addCount] = txtADDRESS.Text;
telephoneNumber[addCount] = Convert.ToInt32(txtTELEPHONE.Text);
email[addCount] = txtEMAIL.Text;
addCount++;
}

private void btnSAVE_Click(object sender, EventArgs e)
{
XmlTextWriter w = new XmlTextWriter(fn, Encoding.UTF8);
w.Formatting = Formatting.Indented;
w.WriteStartDocument();
w.WriteStartElement("employees");
for (int i = 0; i < addCount; i++)
{
w.WriteStartElement("employees");

w.WriteElementString("employeeID", employeeID[i].ToString());
w.WriteElementString("employeeName", employeeName[i]);
w.WriteElementString("jobTitle", jobTitle[i]);
w.WriteElementString("address", address[i]);
w.WriteElementString("telephoneNumber", telephoneNumber[i].ToString());
w.WriteElementString("email", email[i]);
w.WriteEndElement();
} w.WriteEndElement();
w.WriteEndDocument();
w.Close();
Application.Exit();

}

private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists(fn))
{
XmlTextReader r = new XmlTextReader(fn);
r.WhitespaceHandling = WhitespaceHandling.None;
while (r.Name != "employees")
r.Read();
while (r.Name == "employees")
{
r.ReadStartElement("employees");
employeeID[addCount] = Convert.ToInt32(r.ReadElementString("employeeID"));
employeeName[addCount] = r.ReadElementString("employeeName");
jobTitle[addCount] = r.ReadElementString("jobTitle");
address[addCount] = r.ReadElementString("address");
telephoneNumber[addCount] = Convert.ToInt32(r.ReadElementString("telephoneNumber"));
email[addCount] = r.ReadElementString("email");
r.ReadEndElement();
addCount++;

} r.Close();
DisplayRec();
}
}

private void DisplayRec()
{
txtEI.Text = employeeID[currentRec].ToString();
txtNAME.Text = employeeName[currentRec];
txtJOB.Text = jobTitle[currentRec];
txtADDRESS.Text = address[currentRec];
txtTELEPHONE.Text = telephoneNumber[currentRec].ToString();
txtEMAIL.Text = email[currentRec];
lblRECORD.Text = (currentRec + 1).ToString() + "/" + addCount.ToString();

}

private void btnBACK_Click(object sender, EventArgs e)
{
if (currentRec > 0)
currentRec--;
DisplayRec();
}

private void btnNEXT_Click(object sender, EventArgs e)
{
if (currentRec < addCount - 1)
currentRec++;
DisplayRec();
}

private void btnCLEAR_Click(object sender, EventArgs e)
{
txtEI.Clear();
txtNAME.Clear();
txtJOB.Clear();
txtADDRESS.Clear();
txtTELEPHONE.Clear();
txtEMAIL.Clear();

}
}

最佳答案

1. txtNAME should not be left blank,

要识别名称是否为空、空或空白,您可以使用 String.IsNullOrWhiteSpace()方法。

来自 MSDN:String.IsNullOrWhiteSpace()

Indicates whether a specified string is null, empty, or consists only of white-space characters.

试试这个:

if(!String.IsNullOrWhiteSpace(txtNAME.Text))
{
//continue
}
else
{
MessageBox.Show("Error");
}

2. txtTELEPHONE should be at least 10 digits,

您可以使用字符串的Length属性来识别电话号码是否有10位数字。

试试这个:

if(txtTELEPHONE.Text.Length>=10)
{
//continue
}
else
{
MessageBox.Show("Error");
}

3. txtEMAIL should be in email format with "@" and "."

为了验证 EmailID,我建议您使用 RegEx 而不是仅检查 @. 字符。

试试这个:正则表达式

  Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(txtEMAIL.Text);
if (match.Success)
{
//continue
}
else
{
MessageBox.Show("Error");
}

尝试一下:如果您只想检查 @. 字符

if(txtEMAIL.Text.Contains("@") && txtEMAIL.Text.Contains(".") )
{
//continue
}
else
{
MessageBox.Show("Error");
}

关于c# - 使用 C# 进行输入验证和异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com