|
|
|
|
|
Home
»
ASP.NET
»
Client Side Validation with JavaScript in ASP.NET
|
|
|
|
Page Views :
|
38145
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
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" />
- Email : <asp:TextBox ID="txtEmail" />
- Web URL : <asp:TextBox ID="txtWebUrl" />
- 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"); document.getElementById("<%=txtName.ClientID%>").focus(); 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); if(matchURL==null) { 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"); document.getElementById("<%=txtZIP.ClientID%>").focus(); return false; } var digits="0123456789"; var temp; for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++) { temp=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1); if (digits.indexOf(temp)==-1) { alert("Please enter correct zip code"); document.getElementById("<%=txtZIP.ClientID%>").focus(); return false; } } return true; } </script>
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
Now you will get a form with proper validation.
I hope this is going to help you.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Puran Kaushal
Puran Chand Kaushal is working with .NET technology. PCK background includes Master's in Computer Science and Applications . Currently he is exploring C#, ASP.NET, WCF,Design Patterns and SOA.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|