Compare two input fields c # CompareValidator

1

I have a form that contains two fields input , one for the email and another to re-enter the mail and validate that they are the same, for this last I use the CompareValidator but it does not show me the error when switching focus to the send button

The following is the Email field

<asp:Label ID="Label3" Text="Correo electrónico" ToolTip="Correo electrónico" runat="server" /><span class="campoObligatorio">*</span>
<input id="txtCorreo" type="text" maxlength="100" placeholder="Ej: [email protected]" title="Correo electrónico" runat="server" autocomplete="off" validationgroup="vgrCapturaDatos" />
<asp:RequiredFieldValidator ID="rfvCorreo" CssClass="errorCaptura" ValidationGroup="vgrCapturaDatos" Text=" " ErrorMessage="El Correo electrónico es obligatorio" ControlToValidate="textoCorreo" runat="server" />
<asp:RegularExpressionValidator ID="regexEmailValid" CssClass="errorCaptura" runat="server" ValidationGroup="vgrCapturaDatos" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="textoCorreo" Text=" " ErrorMessage="Formato de correo no valido"></asp:RegularExpressionValidator>

The following is the field to be validated

<asp:Label ID="Label6" Text="Valide su Correo electrónico" ToolTip="Correo electrónico" runat="server" /><span class="campoObligatorio">*</span>
<input id="txtValidaCorreo" type="text" maxlength="100" placeholder="Ej: [email protected]" title="Valida correo electrónico" runat="server" autocomplete="off" validationgroup="vgrCapturaDatos"/>

and the following is the validator to compare

<asp:CompareValidator ID="cfvNumeroCelular" runat="server"  ControlToCompare="txtCorreo" ControlToValidate="txtValidaCorreo" Operator="Equal" Type="Integer" Display="Dynamic" ValidationGroup="vgrCapturaDatos" ErrorMessage="El valor ingresado no coincide." ></asp:CompareValidator>

However when I pass the focus to the next control or send the form, it does not validate and passes right so the values are different. I appreciate your help and collaboration to be able to know where I have the error.

    
asked by isaac 12.03.2018 в 23:41
source

1 answer

2

The problem is in the validator compare.

<asp:CompareValidator ID="cfvNumeroCelular" runat="server"  ControlToCompare="txtCorreo" ControlToValidate="txtValidaCorreo" Operator="Equal" Type="Integer" Display="Dynamic" ValidationGroup="vgrCapturaDatos" ErrorMessage="El valor ingresado no coincide." ></asp:CompareValidator>

You have Type="Integer" . Change it to Type="String" . Because you're comparing chains, not integers.

You should also put Display="Static", more than anything to make it look like the others.

On the submit button, put this ValidationGroup="vgrDataCapture" to make sure you validate that group.

I'll give you a code example where it works correctly:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" ControlToCompare="TextBox2" ControlToValidate="TextBox1" ValidationGroup="vgrCapturaDatos"></asp:CompareValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="vgrCapturaDatos"></asp:RequiredFieldValidator>

    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="vgrCapturaDatos" />

Greetings.

    
answered by 13.03.2018 / 00:26
source