controles de validacion
DESCRIPTION
aspTRANSCRIPT
CONTROLES DE VALIDACION PARTE II
APLICACIÓN
Diseñe una aplicación web en c Sharp para mostrar el uso de los controles RequireFieldValidator, RegularExpressionValidator y Custom Validator.
Solución
a. Diseño
Personalización de propiedades de los controles estándar
Control ID Text
TextBox1 txtnom
TextBox2 txtapat
TextBox3 txtamat
TextBox4 txtemail
TextBox5 txttelf
TextBox6 txtusuario
TextBox7 txtpass
TextBox8 txtnum
Button1 Button1 Enviar
Personalización de las propiedades de los controles de validación
Pasos:
1. Efectúe un clic en el control RegularExpressionValidator1
2. En la ventanas de propiedades del control seleccionado, modifique el valor de la propiedad ErrorMessage por el siguiente texto:
Sólo letras…
3. Desde la ventana de propiedades, seleccione la propiedad ValidationExpression, tal como se muestra en la figura.
4. A continuación, digite la expresión regular apropiada.
[A-Z]+[a-z]*
5. En la propiedad ControlToValidate seleccione la caja de texto correspondiente, en este caso, txtnom.
6. Proceda en forma análoga con los controles de validación restantes de acuerdo a la siguiente tabla.
Control ErrorMessage ValidationExpression
ControlToValidate
RegularExpressionValidator2
Sólo letras [A-Z]+[a-z]* txtapat
RegularExpressionValidator3
Sólo letras [A-Z]+[a-z]* txtamat
RegularExpressionValidator4
Dirección inválida
.+@.+ txtemail
RegularExpressionValidator5
Solamente dígitos
\d{7} txttelf
RequiredFieldValidator1
No puede ser nulo
txtusuario
RegularExpress Mínimo 4 \w{4,10} txtpass
ionValidator6 hasta 10 caracteres
CustomValidator1
El numero no es divisible por 6
txtnum
b. Código
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PROY_VALIDACION_CSHARP._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <style type="text/css"> .style1 { width: 115px; } .style2 { height: 20px; } .style4 { height: 20px; width: 114px; } .style6 { width: 229px; } .style9 { height: 20px; width: 230px; } .style10 { width: 114px; height: 23px; } .style11 { width: 230px; height: 23px; } .style12 {
height: 23px; } .style13 { width: 114px; } .style14 { width: 230px; } </style></head><body> <form id="form1" runat="server"> <div> </div> <table style="width: 120%;"> <tr> <td class="style1"> <asp:Label ID="Label1" runat="server" Text="Nombres"></asp:Label> </td> <td class="style6"> <asp:TextBox ID="txtnom" runat="server" Width="204px"></asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Sólo letras" ValidationExpression="[A-Z]+[a-z]*" ControlToValidate="txtnom"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style1"> <asp:Label ID="Label2" runat="server" Text="Apellido Paterno"></asp:Label> </td> <td class="style6"> <asp:TextBox ID="txtapat" runat="server" Width="202px"></asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Sólo letras" ValidationExpression="[A-Z]+[a-z]*" ControlToValidate="txtapat"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style1"> <asp:Label ID="Label3" runat="server" Text="Apellido Materno"></asp:Label> </td> <td class="style6">
<asp:TextBox ID="txtamat" runat="server" Width="200px"></asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Sólo letras" ValidationExpression="[A-Z]+[a-z]*" ControlToValidate="txtamat"></asp:RegularExpressionValidator> </td> </tr> </table> <table style="width: 124%;"> <tr> <td class="style13"> <asp:Label ID="Label4" runat="server" Text="E-Mail"></asp:Label> </td> <td class="style14"> <asp:TextBox ID="txtemail" runat="server" Width="228px" ontextchanged="txtemail_TextChanged"></asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Dirección inválida" ValidationExpression=".+@.+" ControlToValidate="txtemail"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style4"> <asp:Label ID="Label5" runat="server" Text="Telefono"></asp:Label> </td> <td class="style9"> <asp:TextBox ID="txttelf" runat="server"></asp:TextBox> </td> <td class="style2"> <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ErrorMessage="Solamente dígitos" ValidationExpression="\d{7}" ControlToValidate="txttelf"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style13"> <asp:Label ID="Label6" runat="server" Text="Usuario"></asp:Label> </td> <td class="style14"> <asp:TextBox ID="txtusuario" runat="server" ontextchanged="txtusuario_TextChanged"></asp:TextBox> </td> <td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="No puede ser nulo" ControlToValidate="txtusuario"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style13"> Password </td> <td class="style14"> <asp:TextBox ID="txtpass" runat="server"></asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server" ErrorMessage="Mínimo 4 hasta 10 caracteres" ValidationExpression="\w{4,10}" ControlToValidate="txtpass"></asp:RegularExpressionValidator> </td> </tr> </table> <table style="width: 121%;"> <tr> <td class="style13"> Numero divisible por 6 </td> <td class="style14"> <asp:TextBox ID="txtnum" runat="server"></asp:TextBox> </td> <td> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="El numero no es divisible por 6" ControlToValidate="txtnum" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator> </td> </tr> <tr> <td class="style10"> </td> <td class="style11"> </td> <td class="style12"> </td> </tr> <tr> <td class="style13"> <asp:Button ID="Button1" runat="server" Text="Enviar" onclick="Button1_Click" /> </td> <td class="style14"> </td> <td> </td>
</tr> </table> </form></body></html>
c. Código de la clase
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;
namespace PROY_VALIDACION_CSHARP{ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { try { int val = Int32.Parse(args.Value); if (val % 6 == 0) { args.IsValid = true; } else { args.IsValid = false; } } catch { args.IsValid = false; } }
protected void Button1_Click(object sender, EventArgs e) {
AGREGUE ESTE SEGMENTO DE CÓDIGO EN EL EVENTO DEL CONTROL CUSTOMVALIDATOR1
}
protected void txtemail_TextChanged(object sender, EventArgs e) {
}
protected void txtusuario_TextChanged(object sender, EventArgs e) {
} }}
Ejecución
Practica---
Diseñe una aplicación web que permita ingresar y validar los siguientes datos nombres, apellido paterno,apellido materno, fecha de nacimiento ,edad,numero telefónico,dirección de
correo electrónico,domicilio,nacionalidad,lugar de nacimiento,observación.
Reglas de validación… los nombres y apellidos deben aceptar el siguiente formato
-Xxxxx Yyyy
La fecha debe tener el siguiente formato
- Dd/mm/yyyy
La fecha de nacimiento debe aceptar el siguiente formato
-dd/mm/yyyy/
La nacionalidad el lugar de nacimiento y la observación deben estar en mayúscula
Todos los controles deben tener un mensaje de error