Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear friends,
I have the following JQuery code in asp.net web form:-


function ValidateFields(id) {
$("#Rpt1").find("input[type=text]").each(function () {
if ($.trim($(this).val()) == '') {
alert("At least one textbox is empty");

$(this).focus(); // focus the element
return false;
}
return false;

})
}



and on asp.net button click I have the following code:-
<asp:Button ID="BtnSubmit" runat="server" OnClientClick="if
(ValidateFields('BtnSubmit') == false) return(false);"
OnClick="BtnSubmit_Click" Text="Submit" />

The problem is it always fires the onclick event whether the condition is true or false.

What I have tried:

I tried many things
like
OnClientClick="ValidateFields('BtnSubmit');"
and
OnClientClick="ValidateFields('BtnSubmit'); return false;"

but nothing works.

Please help.
Posted
Updated 30-Sep-18 22:35pm

1 solution

Your "return false" is making the function inside your "each" method return false, it isn't making the ValidateFields function return false. In the update below I set an isValid variable that is updated by the inner function in "each" and that variable is then returned from ValidateFields.

<script type="text/javascript">
    function ValidateFields(id) {
        var isValid = true;
        $("#Rpt1").find("input[type=text]").each(function () {
            if ($.trim($(this).val()) == '') {
                alert("At least one textbox is empty");


                $(this).focus(); // focus the element
                isValid = false;
            }
        });

        return isValid;
    }

</script>


For multiple control types

function ValidateFields(id) {
    var isValid = true;
    $("#Rpt1").find("input[type=text], input[type=file], .Select1").each(function () {
        var el = $(this);
        switch (this.tagName.toLowerCase()) {
            case "input":
                if (el.val() == '') {
                    if(el.attr('type') == 'text')
                        alert("At least one textbox is empty");
                    else if(el.attr('type') == 'file')
                        alert("Select a file");
                    isValid = false;
                }
                break;
            case "select":
                if (el.val() == '0') {
                    alert("Select a value");
                    isValid = false;
                }
                break;
        }
        if (!isValid) {
            return false;
        }
    });

    return isValid;
}
 
Share this answer
 
v2
Comments
Madhukar Krishna 1-Oct-18 5:04am    
Hi there,
Thank you dear.Perfect solution. Can You give me the code for stopping at first textbox which is empty.
F-ES Sitecore 1-Oct-18 5:23am    
If you add "return false;" after the "isValid = false;" line then that should break the each loop.
Madhukar Krishna 4-Oct-18 9:09am    
Hi F-ES Sitecore. I have the following code and I want it to stop for the first of first type of control which is empty:

//Repeater
var isValid = true;
$("#Rpt1").find("input[type=text]").each(function () {
if ($.trim($(this).val()) == '') {
alert("At least one textbox is empty");
$(this).focus(); // focus the element
isValid = false;
return false;
}

})

$("#Rpt1").find(".Select1").each(function () {
if ($.trim($(this).val()) == '0') {
alert("select a value");
$(this).focus(); // focus the element
isValid = false;
return false;
}

})

$("#Rpt1").find("input[type=file]").each(function () {
if ($.trim($(this).val()) == '') {
alert("select a file");
$(this).focus(); // focus the element
isValid = false;
return false;
}

})
return isValid;


How to do it? I will be thankful if you answer this!!!
F-ES Sitecore 4-Oct-18 10:47am    
I'd probably get all the components then do all the validation in the same loop. I'll update the solution with the code.
Madhukar Krishna 4-Oct-18 13:36pm    
Great Sitecore! You are a genius. Thank you so much! This works for me!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900