3k views
CustomValidation ASP.NET
Interfaz
- Caja de Texto (TextBox)
- Control CustomValidator
- Propiedades:
- ErrorMessage (mensaje a mostrar si falla la validación)
- ClientValidationFunction (nombre de la función javascript en cliente)
- ControlToValidate (id de la caja de texto)
- Propiedades:
Parte cliente (por ejemplo validando que el número introducido debe ser par)
Javascript
- function esPar(origen, args){
- if (args.Value % 2 == 0)
- args.isValid = true;
- else
- args.isValid = false;
- }
Parte servidor, programación del evento ServerValidate del control
C#
- CustomValidator
- protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
- {
- try
- {
- int numero;
- numero = int.Parse(args.Value);
- args.IsValid = numero % 2 == 0;
- }
- catch (Exception)
- {
- args.IsValid = false;
- throw;
- }
- }
Submitted by jfrank about 1 year ago — last modified less than a minute ago