Friday, June 06, 2008

Javascript Validation - ASP.NET

This simple explanation will help you to implement more and pop up logics to work out.

Go to design page of your page and place the script tag between head tag.

I wanted to check my text box had value or not.
I name it txtAmount and then validation starts here,

function CheckValues()
{
if(document.getElementById("<%=txtAmount.ClientID%>")=="")
{
alert("You forgot to mention your deposit amount!");
document.getElementById("<%=txtAmount.ClientID%>").focus();

return false;
}
return true;
}
}

Now I completed the Validation part,Now I need to register this validation accepted by the server,so go to Code behind file

Add this line on OnClick Events of the Button

Button1.attributes.Add("OnClick","return CheckValues()");