Search code examples
asp.netvalidationrequiredfieldvalidatorcustomvalidator

ASP.NET Required Field Validator & Custom Validator not working


This is my aspx code

<p class="input-block last">
    <asp:TextBox ID="uxEmployerName" runat="server" CssClass="uxemail" TabIndex="10" ValidationGroup="EmployerRegister" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="uxEmployerName" ErrorMessage="You must enter Company Name." EnableClientScript="false" Display="none" ValidationGroup="EmployerRegister" />
</p>
<p class="input-block last">
    <asp:TextBox ID="uxEmail" runat="server" CssClass="uxemail" TabIndex="16" ValidationGroup="EmployerRegister" />
    <asp:RequiredFieldValidator ID="uxEmailValReq" runat="server" ControlToValidate="uxEmail" ErrorMessage="You must enter Email." EnableClientScript="false" Display="none" ValidationGroup="EmployerRegister" />
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="The 'Email Address' you have entered does not appear to be a valid address." ControlToValidate="uxEmail" EnableViewState="False" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="EmployerRegister" Display="None" />
</p>
<p class="input-block last">
    <asp:LinkButton runat="server" ID="uxRegisterButton" Text="Submit" CausesValidation="true" ValidationGroup="EmployerRegister" CssClass="button" TabIndex="18" />
    <a class="button" href="#" tabindex="17">Edit package</a>
</p>

the only validator works here is RegularExpressionValidator

if I input wrong email format then it shows the error message. but not if i leave the email field.

What am i doing wrong. any solution

i have also added a custom validator with the email field

<asp:CustomValidator ID="uxEmailValUnique" runat="server" ErrorMessage="It appears that you already have an account with us. <br/><a href='forgotten-password.aspx'>Request password</a>"
        ControlToValidate="uxEmail" EnableViewState="False" ValidationGroup="EmployerRegister"
        Display="None" />

the backend server code for this is

Private Sub uxEmailValUnique_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles uxEmailValUnique.ServerValidate
    args.IsValid = Not LocalHelper.EmailExists(uxEmail.Text)
End Sub

where in LocalHelper file

Shared Function EmailExists(email As String) As Boolean
    Return DB.GetInteger("select count(id) from [user] where deleted = 0 and active = 1 and email = @email", DB.SIP("email", email)) > 0
End Function

this does not work either

what am i doing wrong.


Solution

  • I assume that you have a ValidationSummary control similar to this:

    <asp:ValidationSummary runat="server" ForeColor="Red" ValidationGroup="EmployerRegister" />
    

    If any of your validators has client-side validation enabled (EnableClientScript="true", the default value), it will prevent the postback when the client validation fails, so that server-side validation will not be performed. In your current code, the RegularExpressionValidator has client-side validation enabled (not explicitly disabled, in fact); if it fails, the server-side validation for the required fields is not performed and the other error messages are not displayed.

    You could disable the client-side validation for all your validators. The message would appear for all of them (if appropriate) every time you submit the form.

    <asp:RequiredFieldValidator ID="uxEmployerNameValReq" EnableClientScript="false" ... />
    <asp:RequiredFieldValidator ID="uxEmailValReq" EnableClientScript="false" ... />
    <asp:RegularExpressionValidator ID="uxEmailExprVal" EnableClientScript="false" ... />
    <asp:CustomValidator ID="uxEmailValUnique" EnableClientScript="false" ... />
    

    Another reasonable approach could be to enable the client-side validator for the most basic requirements, and disable it for the CustomValidator. The more advanced validation does not make much sense if the e-mail is empty or not valid in the first place.

    <asp:RequiredFieldValidator ID="uxEmployerNameValReq" EnableClientScript="true" ... />
    <asp:RequiredFieldValidator ID="uxEmailValReq" EnableClientScript="true" ... />
    <asp:RegularExpressionValidator ID="uxEmailExprVal" EnableClientScript="true" ... />
    <asp:CustomValidator ID="uxEmailValUnique" EnableClientScript="false" ... />
    

    N.B. In your question, you say that the validation message is not displayed when the e-mail field is empty. I haven't seen that behavior with your original code in my tests.