title: Input validation and exceptions with C#
body: <p>I am trying to validate input and handle exceptions in this program. Validating the following: strings in txtNAME should not be left blank, telephone number in txtTELEPHONE should be at least 10 digits, and txtEMAIL should be in email format with "@" and "." How do I validate these inputs and also handle exceptions in case the wrong input is entered? </p>
  
  <pre><code>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 &lt; 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 &gt; 0)
              currentRec--;
          DisplayRec();
      }
  
      private void btnNEXT_Click(object sender, EventArgs e)
      {
          if (currentRec &lt; addCount - 1)
              currentRec++;
          DisplayRec();
      }
  
      private void btnCLEAR_Click(object sender, EventArgs e)
      {
          txtEI.Clear();
          txtNAME.Clear();
          txtJOB.Clear();
          txtADDRESS.Clear();
          txtTELEPHONE.Clear();
          txtEMAIL.Clear();
  
      }
  }
  </code></pre>