Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have done Client Side validation through Java Script. Validation works Fine . but after validation Server code also get exceuted. i want to stop the server code.

Ex:

C#
function validate_form ( )
        {

            var  valid = true;
            if ( document.getElementById("ctl00_ContentPlaceHolder1_txtAssetDescription").value == "" )
            {
                alert ("Please Enter Asset Description");
                txtAssetDescription.focus();
                return valid ;
            }
retutn valid; 
}




Server Code:
VB
<asp:Button ID="btnSave" runat="server" Text="Save" Width="75px"
                       OnClientClick="validate_form()"
                      onclick="btnSave_Click"/>




Now Validation Works but the code written in btnSave also get executed, how to stop that?


Regards,
Pal
Posted

Hi
your java-script function returns true when its get executed. so your page will get post back. modify you java-script function in such a way that it will returns false when there is alert message.

function validate_form ( )
        {
 
            var  valid = true;
            if ( document.getElementById("ctl00_ContentPlaceHolder1_txtAssetDescription").value == "" )
            {
                alert ("Please Enter Asset Description");
                txtAssetDescription.focus();
                return false;
            }
retutn valid; 
}


Hope this will help :)

Thanks
Vinod
 
Share this answer
 
Comments
Amir Mahfoozi 26-Dec-11 0:19am    
+5
IN ONclientClick use return btnsaveclick ()... as shown

VB
<asp:Button ID="btnSave" runat="server" Text="Save" Width="75px"
                       OnClientClick="return validate_form()"
                      onclick="btnSave_Click"/>




in ur function u have to return it as false

C#
function validate_form ( )
        {


return false;
}
 
Share this answer
 
try replace your code with these


C#
function validate_form ( )
        {

            var  valid = true;
            if ( document.getElementById("ctl00_ContentPlaceHolder1_txtAssetDescription").value == "" )
            {
                valid = false;
                alert ("Please Enter Asset Description");
                txtAssetDescription.focus();
                return valid ;
            }
return valid;
}



VB
<asp:Button ID="btnSave" runat="server" Text="Save" Width="75px"
                       OnClientClick="javascript:return validate_form();"
                      onclick="btnSave_Click"/>
 
Share this answer
 
v4
Set the variable to false as below
var  valid = false;
 
Share this answer
 
you can remove the OnClick Attribute So, if page is post back then save_Click function will not call at server side.

<asp:button id="btnSave" runat="server" text="Save" width="75px"
                      OnClientClick="return validate_form()"
                     /></asp:button>


and you can use input tag for display button so it will not post back when you will click on it.

<input type="button" value="Save"  önclick="return validate_form()" style="width:75px" />


Please let me know is this solution helpful to you.
 
Share this answer
 
v2
Change below code

C#
if ( document.getElementById("ctl00_ContentPlaceHolder1_txtAssetDescription").value == "" )
            {
                alert ("Please Enter Asset Description");
                txtAssetDescription.focus();
                return false; //<== Use false in place of valid
            }
 
Share this answer
 

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