locked
CustomValidator OnServerValidate is not running RRS feed

  • Question

  • User858710965 posted

    Hi All,

    I have a textbox for email address, First I want to validate the format of the email address and the check from database that the given email address is available. Now at first I had a required field validator and regular expression validator and then for checking email address from database I am going to use Custom Validator. Now the real problem that I am facing is that OnServerValidate event is not running and I have no idea what is wrong with it. 

    <tr>
    	<td>
    		<asp:TextBox ID="txtEmpNumber" CausesValidation="true" AutoPostBack="true"  runat="server" CssClass="standardTextBox" ValidationGroup="PageLevel"
                        MaxLength="50"></asp:TextBox>
    		<asp:CustomValidator ErrorMessage="errormessage" ValidationGroup="PageLevel" ControlToValidate="txtEmpNumber" OnServerValidate="EmailValidate" Text="Please enter a valid email address"
                                                                runat="server" />
    			<asp:UpdatePanel ID="updatePanelCheckUserName" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Label ID="lblValidUserId" EnableViewState="false" runat="server" CssClass="ValidationError"></asp:Label>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="txtEmpNumber" EventName="TextChanged" />
                    </Triggers>
                </asp:UpdatePanel>
        </td>
        <td>
            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
            <asp:RegularExpressionValidator EnableViewState="false" ID="regValidator1" runat="server"
                ControlToValidate="FileUpload1" ValidationExpression="[a-zA-Z0_9].*\b(.jpg|.JPG|.gif|.GIF|.jpeg|.JPEG|.png|.PNG)\b"
                ErrorMessage="Invalid image file. Please select a gif, jpeg, jpg or png file."
                EnableClientScript="true" Display="Dynamic" CssClass="ValidationError">
            </asp:RegularExpressionValidator>
        </td>
    </tr>
    protected void EmailValidate(object source, ServerValidateEventArgs args)
    {
           args.IsValid = false;
    }


    Any help would be greatly appriciated. 

    Thanks,

    Attiqe

    Monday, March 4, 2013 7:51 AM

Answers

  • User858710965 posted

    Thanks guys, I actually solved it by just changeing the ValidationGroup. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2013 4:31 AM

All replies

  • User235106362 posted

    With a custom validator I think you need to check if the page is valid in the submit code.

    if (Page.IsValid)
    {
       //do whatever...
    }



    Monday, March 4, 2013 7:57 AM
  • User-760709272 posted

    Add this attribute to your validator

     ValidateEmptyText="True"

    Monday, March 4, 2013 7:58 AM
  • User858710965 posted

    AidyF,

    I have added but doesn't seems to work. Any other thoughts?

    Thanks,

    Attiqe

    Monday, March 4, 2013 8:06 AM
  • User858710965 posted

    Crowcoder,

    I want to check to verify the avilalabilty of email address as user entered the email address. 

    Thanks,

    Attiqe

    Monday, March 4, 2013 8:07 AM
  • User-760709272 posted

    Try adding this too

    EnableClientScript="false"

    If still not working try

    Page.Validate();
    if (Page.IsValid)
    {
    ...
    }



    Monday, March 4, 2013 8:13 AM
  • User235106362 posted

    Yes, Page.IsValid is set by your validators. Any one that is invalid will set the page invalid.

    Monday, March 4, 2013 8:13 AM
  • User858710965 posted

    I added the 

    <asp:CustomValidator ErrorMessage="errormessage" EnableClientScript="false" ControlToValidate="txtEmpNumber" ValidateEmptyText="true" OnServerValidate="ValidateEmailAddress"
                                                                runat="server" />

    Where should I add the Page.Validate(); of Page.IsValid becuase ValidateEmailAddress event is not running. 

    Thanks,

    Attiqe

    Monday, March 4, 2013 8:25 AM
  • User-1949524191 posted

    I had the same problem long time ago and I gave up. I tried a lot of solutions without success. Maybe you should think about CustomValidator with ClientValidationFunction. What do you think?

    Monday, March 4, 2013 8:25 AM
  • User858710965 posted

    Sounds good and I tried clientvalidate function that is running but real problem is that how to access my database from cleint side?

    Monday, March 4, 2013 8:28 AM
  • User-1949524191 posted

    Maybe instead of CustomValidator try use textbox's event "TextChanged" and label or literal for displaying messages. The event fires when text has been changed (I mean after entered in textbox).

    Rgds

    Monday, March 4, 2013 8:49 AM
  • User858710965 posted
    <asp:TextBox ID="txtEmpNumber" CausesValidation="true" runat="server" CssClass="standardTextBox" OnTextChanged="txtEmpNumber_TextChanged"
                                                                AutoPostBack="true" ValidationGroup="PageLevel" MaxLength="50"></asp:TextBox>
                                                            <asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="txtEmpNumber"
                                                                EnableClientScript="true" Display="Dynamic" ErrorMessage="*" CssClass="ValidationError"
                                                                ValidationGroup="PageLevel" SetFocusOnError="True" EnableViewState="false"></asp:RequiredFieldValidator>
                                                            
                                                            <br />
                                                            <asp:RegularExpressionValidator ID="regEmpNbr" runat="server" ControlToValidate="txtEmpNumber"
                                                                CssClass="ValidationError" Display="Dynamic" ErrorMessage="Invalid Email ID format"
                                                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="PageLevel"
                                                                SetFocusOnError="true"></asp:RegularExpressionValidator>

    I have chnaged my code according to your request even now textchange event is not running? 

    Monday, March 4, 2013 8:55 AM
  • User-1949524191 posted

    Could you provide code for txtEmpNumber_TextChanged function/event? Why are you using Validators? Do you know how to initialize events to controls?

    Monday, March 4, 2013 11:21 AM
  • User858710965 posted

    Thanks guys, I actually solved it by just changeing the ValidationGroup. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2013 4:31 AM