Resources » Code Snippets » ASP.NET WebForms

Client Side Validation with JavaScript in ASP.NET

Updated: Category: ASP.NET WebForms
Author: Member Level: GoldPoints: 10

This simple program will guide how to do client side validation of Form in JavaScript.

In this just make a form as follows:

1. Name : <asp:TextBox ID="txtName" />
2. Email : <asp:TextBox ID="txtEmail" />
3. Web URL : <asp:TextBox ID="txtWebUrl" />
4. Zip : <asp:TextBox ID="txtZip" />

<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />

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");
return false;
if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
alert("Email id can not be blank");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
alert("Zip Code is not valid");
return false;
var digits="0123456789";
var temp;
for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++)
if (digits.indexOf(temp)==-1)
alert("Please enter correct zip code");
return false;
return true;

And in code behind file just write the below code.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnSubmit.Attributes.Add("onclick", "return validate()")
End Sub

Did you like this resource? Share it with your friends and show your love!

Responses to "Client Side Validation with JavaScript in ASP.NET "
Author: Jagadevi basawaraj  28 May 2008   Member Level: Silver   Points : 2
What is ClientID ?

Author: Ashwini Rajpurohit  17 Jun 2008   Member Level: Bronze   Points : 1
This was really very helpful for me in my project...the only thing missing is the validation for valid date..if u can please add it also

Author: Dotnet Developer-2015  01 Sep 2010   Member Level: Diamond   Points : 1
hi komaladevi
excellent copy then paste this site

Author: faizulla  09 Mar 2015   Member Level: Bronze   Points : 2
this code works fine in one page

for other page i have to write # insted of = in get by element id
can any one help to find out the diffrence between (#) and (=).
in our project we are using springs.


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India