简体   繁体   中英

OnClick doesn't work after Jquery validations

I have given validations through jquery which works fine on button click. But as soon as I fill all the data into the page, my onClick does not fires. I dont know why. Please suggest.

Here is my jquery code:-

var ErrArr = [];
    $(document).ready(function () {
        $('#btnSave').click(function (e) {
            e.preventDefault();
            validateTitle();
            validateddl();
            validateTextBoxes();
            if (ErrArr.length > 0) {
                alert(ErrArr.join("\n"));
                ErrArr = [];
                return false;
            }
        });
        function validateTitle() {
            if ($("#ddlTitle").val() > "0") {
                if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
                    ErrArr.push("Please enter the text in other title");
                }
            } else {
                ErrArr.push('Please select the title');
            }
        }
        function validateTextBoxes() {
            if ($("#txt_emp_fname").val() === "") {
                ErrArr.push('First name is required');
            }
            if ($("#txt_emp_mname").val() === "") {
                ErrArr.push('Middle name is required');
            }
            if ($("#txt_emp_lname").val() === "") {
                ErrArr.push('Last name is required');
            }

            if ($("#txtDateofJoin").val() === "") {
                ErrArr.push('Date of joining is required');
            }
            if ($("#txtReligion").val() === "") {
                ErrArr.push('Religion is required');
            }
            if ($("#txtBloodGroup").val() === "") {
                ErrArr.push('Blood group is required');
            }
            if ($("#txtPersonalEmail").val() === "") {
                ErrArr.push('Email Id is required');
            }
            if ($("#txtDtOfBirth").val() === "") {
                ErrArr.push('Date of birth is required');
            }
            if ($("#txtAt").val() === "") {
                ErrArr.push('Place of birth is required');
            }
            if ($("#txtTaluka").val() === "") {
                ErrArr.push('Taluka is required');
            }
            if ($("#txtPostOffice").val() === "") {
                ErrArr.push('Post office is required');
            }
            if ($("#txtDistrict").val() === "") {
                ErrArr.push('District is required');
            }
            if ($("#txtStatePersonal").val() === "") {
                ErrArr.push('State is required');
            }
            if ($("#txtDtMarriage").val() === "") {
                ErrArr.push('Date of Marriage is required');
            }
            if ($("#TxtPassportNo").val() === "") {
                ErrArr.push('Passport no is required');
            }
            if ($("#txtIdMark").val() === "") {
                ErrArr.push('Identification mark is required');
            }
        }
        function validateddl() {
            if ($("#ddlGender").val === "" || $("#ddlGender").val() == "0") {
                ErrArr.push('Please select the gender');
            }
            if ($("#ddlMaritalStatus").val === "" || $("#ddlMaritalStatus").val() == "0") {
                ErrArr.push('Please select the Marital Status');
            }
            if ($("#ddlNationality").val === "" || $("#ddlNationality").val() == "0") {
                ErrArr.push('Please select the Nationality');
            }
        }
    }); 

Also see my aspx of button:-

<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server" 
                    CausesValidation="true" onclick="btnSave_Click"
                    />

I suggest you do NOT use the click of the button but the submit of the form to validate:

$(document).ready(function () {
    $('#formID').on("submit",function (e) {
        // e.preventDefault(); // does not belong here
        validateTitle();
        validateddl();
        validateTextBoxes();
        if (ErrArr.length > 0) {
            e.preventDefault();// but here
            alert(ErrArr.join("\n"));
            ErrArr = [];
        }
    });
.
.
.

I assume that also means you should make your button a submit button and make sure the asp does not add a click event since it is already assigned by the jQuery

$('#btnSave').on("click", function(e) {
    //e.preventDefault();
    validateTitle();
    validateddl();
    validateTextBoxes();
    if(ErrArr.length > 0) {
        e.preventDefault(); //use e.preventDefault here
        alert(ErrArr.join("\n"));
        ErrArr = [];
        //return false;
    }
});

move event prevent on the button click handler only when client validation fails.

    $('#btnSave').click(function (e) {

        validateTitle();
        validateddl();
        validateTextBoxes();
        if (ErrArr.length > 0) {
            alert(ErrArr.join("\n"));
            ErrArr = [];
            e.preventDefault();
            return false;
        }
    });

Try this

$('#btnSave').click(function(e) {
    //e.preventDefault();
    validateTitle();
    validateddl();
    validateTextBoxes();
    if(ErrArr.length > 0) {
        e.preventDefault(); //use e.preventDefault here
        alert(ErrArr.join("\n"));
        ErrArr = [];
        //return false;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM