Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client Side Jquery Validation on Button Click in asp.net?

I am trying to validate fields on button click using Jquery but the problem is that it seems that code behind event and jquery event are running in parallel.

(UPDATED)

 <asp:Button ID="btnsave" runat="server" ClientIDMode="Static" OnClientClick="return clientFunction()" CssClass="ButtonText"
                    Text="SAVE" OnClick="btnsave_Click" />
                    &nbsp;

CLIENT SIDE VALIDATION:

<script type="text/javascript">

    function clientFunction() {

        var isValid = true;
        $('#txtsupplieruserid,#txtsuppliername,#txtbusinessemailid,#txtorganizationname,#txtphonenumber,#txtcity,#txtstate,#txtpostalcode,#txtaddress').each(function () {
            if ($.trim($(this).val()) == '') {
                isValid = false;
                $(this).css({
                    "border": "1px solid red",
                    "background": "#FFCECE"
                });
            }
            else {
                $(this).css({
                    "border": "",
                    "background": ""
                });
            }
        });
        if (isValid == false) {

            e.preventDefault();
            return isValid;
        }
        else {
            return isValid;
        }
    }
</script>

I would like to validate through Jquery first than go to code behind if it passes clientside validation. I want to do this using Jquery and not pure Javascript.

like image 203
SamuraiJack Avatar asked Dec 13 '25 22:12

SamuraiJack


1 Answers

You can use OnClientClick for client side validation and if validation passes return true otherwise return false. If true is returned server side function will be called otherwise it won't.

<asp:Button ID="btnsave" runat="server" ClientIDMode="Static" CssClass="ButtonText" Text="SAVE" OnClientClick="return clientFunction()"   onclick="btnsave_Click" />


function clientFunction()
{

    var isValid = true;
    $('#txtsupplieruserid,#txtsuppliername,#txtbusinessemailid,#txtorganizationname,#txtphonenumber,#txtcity,#txtstate,#txtpostalcode,#txtaddress').each(function ()  
    {
             if ($.trim($(this).val()) == '') {
                 isValid = false;
                 $(this).css({
                     "border": "1px solid red",
                     "background": "#FFCECE"
                 });
             }
             else {
                 $(this).css({
                     "border": "",
                     "background": ""
                 });
             }
     });
         if (isValid == false) {
             return false;
         }
         else {
             return true;
         }
}

If your controls are server controls than you may need to use ClientIDMode="Static". So their ID remains same.

like image 99
Mairaj Ahmad Avatar answered Dec 15 '25 12:12

Mairaj Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!