我想驗證輸入並處理這個程序中的異常。驗證以下內容:txtNAME中的字符串不應留空,txtTELEPHONE中的電話號碼至少應爲10位數字,並且txtEMAIL應採用帶「@」和「。」的電子郵件格式。如何驗證這些輸入,並在輸入錯誤的輸入的情況下處理異常?用C輸入驗證和異常#
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();
}
}
您已經包括了很多不必要的代碼。如果你把這個問題提煉出適用於你的問題的代碼,人們會更願意提供幫助。 –
儘管您可能認爲,電話號碼不是一個數字 - 它是一個數字字符序列。具體而言,根據您的位置以及是否包含國家/地區代碼,以數字形式存儲的電話號碼可能會溢出32位整數。最好將它作爲一個字符串來處理。 – tvanfosson