I have a form that contains two fields input
, one for the email and another to re-enter the email and validate that they are the same, for the latter I use the CompareValidator but it does not display the error when I change focus to the submit button
Next 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 submit the form, it doesn't do the validation and passes right through so the values are different. I appreciate your help and collaboration to be able to know where I have the error.
The problem is in the compare validator.
You have set Type="Integer" . Change it to Type="String" . Because you are comparing strings, not integers.
You should also put Display="Static", more than anything so that it is like the others.
In the submit button, also put this ValidationGroup="vgrCapturaDatos" to make sure that it validates that group.
I leave you an example of code where it works correctly:
All the best.