How to validate if a string string already exists in asp.net?

0

I would like to know how I can do to validate in asp.net if a text string entered is equal to another. I am doing it using the ValidationResult method but it does not work, I leave the code.

public class ValidacionPlacaExistente
    {
        public static ValidationResult ValidarPlacaExistente(string ValidarPlacaExistente, ValidationContext validationContext)
        {
            Vehiculo vehiculo = (Vehiculo)validationContext.ObjectInstance;
            return ValidarPlacaExistente.Equals(vehiculo.numeroDePlaca.Length)
                ? new ValidationResult("El número de placa ya existe.")
                : ValidationResult.Success;
        }
    }
    
asked by Diego campos 16.10.2018 в 07:24
source

1 answer

0

To compare two textbox in asp.net there is the control CompareValidator

Validation - CompareValidator

can be validated on the client side, it does not have to be a server-side code

<asp:TextBox runat="server" id="TextBox1" />
<asp:TextBox runat="server" id="TextBox2" />
<asp:CompareValidator runat="server" id="cvIguales" controltovalidate="TextBox1" 
   controltocompare="TextBox2" operator="Equal" type="String" errormessage="No son iguales" />

as you will see it is quite simple and does not require code

CompareValidator Class

If you need to validate from the server side, you would use the control

How to: Validate with respect to values in a database for controls ASP.NET server

The CustomValidator allows you to define an event on the server for validation

    
answered by 16.10.2018 в 18:10