TechShri from Shriniwas Wani

Custom Search

25 May, 2006

JavaScript Validation in ASP.Net

Use following for Client Side Validation with JavaScript in ASP.NET
Reference : http://www.dotnetheaven.com/Uploadfile/purankaushal/103292006010736AM/1.aspx

This simple program will guide how to do client side validation of Form in JavaScript.
In this just make a form as follows:
Name :
<
asp:TextBox ID="txtName" runat="server"/
>


Now on the source code of this form in script tag write the following code:

<

script language="javascript" type="text/javascript"
>
function validate()
{
if (document.getElementById("< % = txtName.ClientID % >").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("< % =txtName.ClientID % >").focus();
return false;
}
}

And in code behind file just write the below code.


protected void Page_Load(object sender, EventArgs e)
{
btnSubmit.Attributes.Add("onclick", "return validate()");
}

Now you will get a form with proper validation.
This way you can also validate the other validations as well, also can use Regular Expressions as well.


I hope this is going to help you.
Shriniwas Wani