Question

I have required field validators on my page that work however they still allow the button to call a java script function. for example i can add text and delete it and the validator will show an error. If click the submit button it still executes the javascript function in onclientclick. How can i stop it from executing the function in the onclientclick?

Button:

<asp:Button ID="formPost" text="Submit" class="btn btn-default" runat="server" CausesValidation="true" OnClientClick="return sendHPCIMsg();" />

Note: I don't have access to edit sendHPCIMsg() function.

Field to validate:

<div class="form-group col-small">
 <label class="col-sm-2 control-label">First Name: </label>
                                            <div class="col-sm-8">
                    <asp:TextBox ID="fNameTxt" runat="server" CssClass="input-small"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidatorFname" ControlToValidate="fNameTxt" runat="server" ErrorMessage="First name is required" ValidationGroup="billing" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </div>
                                        </div>

Also the field above are in a AJAX CollapsiblePanelExtender so here is the javascript I use to enable/disable the validation (Based on a check box):

<script>
        $('#SameBillingChkBx').bind('click', function (e)
        {
            if ($(this).is(':checked'))
            {
                $.each(Page_Validators, function (index, validator) {
                    if (validator.validationGroup == "billing") {

                        ValidatorEnable(validator, false);

                    }
                });
            }
        })
    </script>
Was it helpful?

Solution

I figured it out. I wrote the following function:

function submitform()
{
    if (!Page_IsValid) {
        return false;
    }
    return sendHPCIMsg();
}

Then I called this on my click event.

<asp:Button ID="formPost" text="Submit" class="btn btn-default" runat="server" CausesValidation="true" OnClientClick="return submitform();" />

Thanks everyone for the help!

OTHER TIPS

add: OnClientClick="if(Page_ClientValidate()) CloseDialog();"

<asp:Button ID="formPost" text="Submit" class="btn btn-default" runat="server" CausesValidation="true" OnClientClick="if(Page_ClientValidate()) return sendHPCIMsg();" />

Add Inside function

 var val = <%= RequiredFieldValidatorFname.ClientID %>;
  if (val.isvalid == false) {
   //Do something
 }

this will disable the validator for next Click

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top