Advertisement
Fhernd

Principal.cs

Mar 5th, 2018
1,744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows.Forms;
  4.  
  5. namespace R717ValidarEntradaControl
  6. {
  7.     public partial class Principal : Form
  8.     {
  9.         public Principal()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void btnEnviar_Click(object sender, EventArgs e)
  15.         {
  16.             string mensajeError = string.Empty;
  17.             bool invalidaEntrada = false;
  18.  
  19.             foreach (Control control in Controls)
  20.             {
  21.                 if (!erpValidarEntrada.GetError(control).Equals(String.Empty))
  22.                 {
  23.                     mensajeError += "  * " + erpValidarEntrada.GetError(control) + "\n";
  24.                     invalidaEntrada = true;
  25.                 }
  26.             }
  27.  
  28.             if (invalidaEntrada)
  29.             {
  30.                 MessageBox.Show("El formulario contiene errores: \n\n" + mensajeError,
  31.                     "Datos Inválidos", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  32.             }
  33.             else
  34.             {
  35.                 Close();
  36.             }
  37.         }
  38.  
  39.         private void txtEmail_Leave(object sender, EventArgs e)
  40.         {
  41.             Regex regex = new Regex(@"^[\w-]+@([\w-]+\.)+[\w-]+$");
  42.  
  43.             Control control = (Control) sender;
  44.  
  45.             if (String.IsNullOrEmpty(control.Text) || regex.IsMatch(control.Text))
  46.             {
  47.                 erpValidarEntrada.SetError(control, "");
  48.             }
  49.             else
  50.             {
  51.                 erpValidarEntrada.SetError(control, "Email inválido.");
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement