Bashabi
Bashabi

Reputation: 706

ASP.NET Custom validator for unique entry is not working

I am trying to implement a custom validator to comapare the values in database so that the entry is unique - in my registration page.

This is my aspx page

    <h4>  Sign up  </h4>

    <div class="">
        <div class="status">
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableViewState="false"
                ValidationGroup="EmployerRegister" HeaderText="Sorry we could not process request because:"
                CssClass="error" />
        </div>

        <p class="input-block last">
            <label for="<%=uxEmployerName.ClientId %>">
                <strong>Company Name</strong> (required)
            </label>

            <asp:TextBox ID="uxEmployerName" runat="server" CssClass="uxemail" TabIndex="10"
                ValidationGroup="EmployerRegister" />
            <asp:RequiredFieldValidator ID="uxEmployerNameValReq" runat="server" ControlToValidate="uxEmployerName"
                ErrorMessage="You must enter Company Name." EnableClientScript="true"  Display="Dynamic"
                ValidationGroup="EmployerRegister" />
        </p>


        <p class="input-block last">
            <label for="<%=uxEmail.ClientId %>"> <strong>Your Email</strong> (required) </label>

            <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="true" 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" />

            <asp:CustomValidator ID="uxEmailValUnique" runat="server" OnServerValidate="uxEmailValUnique_ServerValidate"  ErrorMessage="It appears that you already have an account with us."
                ControlToValidate="uxEmail" EnableViewState="False" 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" />

        </p>
    </div>

The server side code in vb.net page 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

the function in LocalHelper file is

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

but the custom validator is not working. its taking duplicate email address and not giving any error message. My database is connected properly.

what is wrong in this code/ how to fix this.

Upvotes: 1

Views: 615

Answers (1)

Sharique Hussain Ansari
Sharique Hussain Ansari

Reputation: 1456

Create an event handler for the CustomValidator, that is missing in your html, control's ServerValidate event like this:-

<asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Names="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

If this doesn't work then on submit event try this:-

Page.Validate()                
If Page.IsValid Then
   'your code 
End If

Your Custom Validator ServerValidate code

Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs)
    args.IsValid = False
End Sub

Hope this will help you.

Upvotes: 1

Related Questions