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" />
-
Button
- <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>
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- btnSubmit.Attributes.Add("onclick", "return validate()")
- End Sub
I hope this is going to help you.

ravi GautamPosted Oct 15, 2017, 3:13 PM
I am getting getelementbyid.value but instead of value i am getting valueOf() method in asp.net. When i write getelemnetbyId.value it getting error please help
bajrangee chauhanPosted Aug 29, 2017, 11:37 AM
1064649954542121121245545 zip code currect
Anu VPosted Aug 27, 2016, 2:01 AM
Nice..
kalu singh raoPosted Jul 9, 2016, 10:23 AM
Nice...
DarinPosted Nov 10, 2015, 4:44 AM
Thanks for the code, buddy !!
SharadPosted Jul 17, 2015, 3:40 AM
good one...
piyusha patravaliPosted Apr 3, 2014, 4:31 AM
Hello Puran Thanks for article but can u pls elaborate the term /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*) )$/; what does its each character mean. so that it will be helpfull for freshers to write such different validations on thier own.
Phearin MeyPosted Apr 1, 2014, 3:32 AM
Thanks very much
Gaurav WagheditedPosted May 8, 2013, 3:34 AMEdited May 8, 2013, 3:48 AM
Thanks..very helpful for me..
Guest UserPosted Aug 13, 2012, 11:52 PM
Nice Program,Help a lot to me.. Keep going
vinay rajeditedPosted Jul 19, 2012, 6:01 AMEdited Jul 19, 2012, 6:01 AM
it was nice comment was nice
Guru RPosted Jun 12, 2012, 5:39 AM
NIce .... Gurujan
Guru RPosted Jun 12, 2012, 5:39 AM
NIce .... Gurujan
Guru RPosted Jun 12, 2012, 5:39 AM
NIce .... Gurujan
chandresh rathorePosted May 29, 2012, 2:43 AM
Nice article sir thanks
Naga PrasanthPosted Apr 10, 2012, 4:22 PM
Hi Puran, Thanks for excellent example, It is soo easy to understan that a learner can easily understand and implement it, i am looking of this kind of expmples in the net and i finally found it, you are doing a great job Puran. Thanks, Naga
vijay kumarPosted Mar 19, 2011, 2:29 PM
Thanks a lot, its really help me
PankaJ DPosted Feb 28, 2011, 8:11 AM
Hello, What is purpose of this btnSubmit.Attributes.Add("onclick", "return validate()")
Victor Del PretePosted Feb 20, 2011, 5:20 AM
This article was extremely helpful for javascript validation routines. When dealing with multiple asp.net user controls on the same screen, the built in validation controls for asp.net are not useful. Therefore javascript must be utilized. Very much appreciated for the useful code. Victor Del Prete
Saroj PandaPosted Oct 30, 2010, 9:59 AM
Thanks alot .
Santoshkumar ghorudePosted Aug 14, 2010, 9:30 AM
hi ths is santosh ghorude pls suget me thanking
Santoshkumar ghorudePosted Aug 14, 2010, 9:27 AM
hi ths is santosh ghorude pls suget me thanking
Santoshkumar ghorudePosted Aug 14, 2010, 9:27 AM
hi ths is santosh ghorude pls suget me thanking
Santoshkumar ghorudePosted Aug 14, 2010, 9:26 AM
hi ths is santosh ghorude pls suget me thanking
Vigneshwar KundarapuPosted May 31, 2010, 6:10 AM
Object doesn't support this property or method iam getting this error when i am using with two text boxes for the 2nd text box i am getting error
rahul choudharyPosted May 27, 2010, 8:34 AM
hi Puran, cav we change color of textbox for show error
Rakesh KumarPosted May 26, 2010, 1:17 PM
Hi Puran, Very good article. Can you pls show Datetime validation (mm/dd/yy hh:mm:ss) . Thanks in advance
madhuri mPosted Apr 21, 2010, 7:20 AM
thank u so much for the validations in java script. can you please tell me what packages are to be included in .cs file for this program
Asha BhattPosted Jan 23, 2010, 4:01 AM
Dear Sir, i see your link this http://www.c-sharpcorner.com/UploadFile/purankaushal/103222006013805AM/1.aspx It a very good code for validation,but actually my program is in asp.net (web application) Please tell me how can i call the function from .aspx page .i want to call this validate function when i will click on my button. Please reply me............... Thank you.
paul walkerPosted Dec 30, 2009, 10:00 AM
Quality example, thanks Paul
Horacio GarciaPosted Dec 29, 2009, 2:29 PM
Can you provide C# version for this: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load btnSubmit.Attributes.Add("onclick", "return validate()") End Sub Regards.
habib rehmanPosted Sep 14, 2009, 1:08 AM
nice way to explaine
JoannePosted Jul 14, 2009, 4:18 PM
in IE, it still submit. How can I fix the problem. Thank you.
chandana bharatieditedPosted Jun 1, 2009, 2:33 AMEdited Jun 1, 2009, 2:35 AM
thanx for the code. very helpful for me. but if i use html control instead of asp control then how should i implement it. please mail me at [email protected] chandana
anil singhPosted Mar 20, 2009, 4:03 AM
how to use required field validation and regular expression when using ajax modelpopup extenter with browser firefox
Muskan AlwaysPosted Mar 12, 2009, 4:58 AM
Hello Puran, Thanks for this article. This is really very helpful. But I have some problem while using it with Ajax Update Panel and Master Page. I am working on Asp.net 3.5 and I am trying to do the same things on a content page which is having a Master page, Ajax Update Panel and Script manager. The error is 'Object Required' Can you please help me if you have anything regarding this. I can send you the code if you want. Any help will be highly appreciated. Thanks
kalandi AcharyaPosted Feb 8, 2009, 8:29 AM
Hi Puran, Thanks a lot for your article which helped me a lot for my validation problem.Its really amazing. Regards Kalandi Charan Acharya [email protected]
niaz aPosted Jan 4, 2009, 11:56 PM
sir can u explain this sentence briefly (document.getElementById("<%=txtName.ClientID%>").value=="") and sir what do u meant by" ispostback"?
pappu pappuPosted Nov 7, 2008, 8:23 AM
how is run the script in database
pappu pappuPosted Nov 7, 2008, 2:25 AM
where i have to put the script tag,in HEAD tag or in BODy tag,plz help me, i need it very badly.. and what is use of global.asax and how it use it in my application.... regard pappu
Naresh ThondepuPosted Nov 3, 2008, 7:37 AM
Hi Puran, Thanks Puran for ur article on Validations.You have covered almost all types of validations and gave us a good resource for me and to all the developers.Thanks again Puran.
Robert DoddPosted Nov 1, 2008, 7:54 PM
Can you tell me how to pass arguments into a function using this technique. For instance I have a function that takes a number of the form elements as arguments. Submit.Attributes.Add("onclick", "return IsValidCreditCard(txtCCNumber, txtCCType,cboExpiresDateMonth, cboExpiresDateYear, txtCCCode)") Where txtCCNumber, txtCCType etc. are a values from the froms collection? thanks
Babak SekandariPosted Aug 13, 2008, 6:42 PM
System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). I get the above error when attempting to use similar code: I have code as shown: function ValidateEntry() { var PONum1; PONum1 = document.getElementById('<%= PONum1.ClientID %>'); ... } ... <asp:TextBox ID="PONum1" Width="100" runat="server" MaxLength="10"></asp:TextBox> In my code behind page Page_Load event, I use this code: btnSubmit.Attributes.Add("onclick", "return ValidateEntry();"); I don't know why I am getting the error. Please help.
kiran kumarPosted Mar 22, 2008, 7:37 AM
how to validate checkbox is it checked or not by using java script for client side scripting.
namita bharambePosted Mar 6, 2008, 12:55 AM
pleas give me javascript validation with c# .net code for phone number.
Rahul PatilPosted Mar 3, 2008, 8:59 AM
i have to validate ftp server path, below are valid paths 1) ftp:\\10.11.16.32\CDR 2) 120.11.25.154 3) ftp.worldroam.uk.co.in 4)c:\\CDR\Test can any one provide a regular expression which will work for me all above 4 states. thanks in advance.
Manoj BishteditedPosted Mar 3, 2008, 1:13 AMEdited Mar 3, 2008, 1:39 AM
Thnx for ur article. But there is a problem actually whenever i'm putting my code in jsfile and it's path in master page then this article is not working properly.........plz help me......
Nag BabPosted Feb 26, 2008, 8:05 AM
I want an image to be displayed only when summary validator shows an error. if there is no errormessage shown summary validator the image should be invisible.Please help me.(Every thing i want it to happen at client side only.....if no errors i want server even to be raised) thanks Vardhan [email protected]
YuvrajPosted Jan 30, 2008, 6:49 AM
how to hide a block of text after clicking on image at client side like what happens in this same page when i click on '?' button all the data below that goes up & vice versa
muthu krishnanPosted Jan 29, 2008, 6:49 AM
var emailRegExp = /^[a-z][a-z_0-9\.]*[a-z0-9]+@[a-z0-9]+[a-z_0-9\.]*[a-z0-9]+\.[a-z]{2,4}$/i. using this kind of regex
sreekanth kolluriPosted Jan 9, 2008, 12:33 AM
hi this is sreekanth this articale is really exlet i hope i am expecting meny more articales like this form u Thanks & Regards Sreekanth Kolluri Software Engineer(.Net) NETSOFT INFORMATICS Pvt Ltd. SCO 441-442, Sector 35-C, Chandigarh (UT) 160022 India Email | [email protected] Voice | 91-172-264-6460, 266-2464 [Board] Fax | 91-172-2646461 [ India ] +1-419-745-0256 [US] Web | www.netsoftglobal.com
Deepak GuptaPosted Jan 7, 2008, 7:46 AM
I have text box which carries Postal Code and this text box is validated by a reg ex validator. Now I have State Drop down box. When I change the state in drop down box I need to revalidate the postal code text box using that .net reg ex validator. Now here my question is how can I handle such scenario. Please me the solution if any. Deepak Gupta
Mohammad InsairatPosted Dec 22, 2007, 11:19 AM
What if i have a delegate ' server side ' on the same button, it shouldn't be called unless the validation returns true?!
Manga laxmikantheditedPosted Sep 26, 2007, 2:56 AMEdited Sep 26, 2007, 3:02 AM
Hi puran, i would like to validate the email id [email protected] but it is not accepting the validation .if there are 2 characters before the "@ "it is valdating but if i have 1 character it is not validating. Thanks in Advance Regards kanth
jatin deotalePosted Sep 12, 2007, 1:36 AM
how to use ajx in asp.net
Jyothirmai bachuPosted Sep 5, 2007, 1:22 AM
wot happens to this client side validator if we block Javascript?
Ben RodriguezeditedPosted Aug 6, 2007, 9:42 AMEdited Aug 6, 2007, 9:46 AM
I would like to know if this code works for asp.net 2.0 ? I am trying to implement this code to my website but is not working with asp.net 2.0 Is this for asp.net 2.0 or 1.0 anyway?
lanceeditedPosted Jul 10, 2007, 8:49 AMEdited Jul 10, 2007, 8:50 AM
Thanks for sharing this excellent code. Although it may be obvious to most I’m not sure how you did the email verification. Could you please explain this line of code for me? /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/; I can tell that before the @ sign you accept all upper and lower case letters but what do the rest of the symbols represent. Also I’m from Canada and our postal code is 6 characters long but alternate Letter Number Letter Number Letter Number Any Ideas on how to validate for this?
Himani SharmaPosted May 29, 2007, 9:14 AM
yours was the only blog that solved my query of accessing the asp.net textbox controls through javascript. i was unable to retreve them using document.getelementbyid() until u suggested a differnet way ("<%=txtZIP.ClientID%>"). truly useful thanks
alex 0Posted Apr 27, 2007, 8:30 AM
hi document.getElementById("<%=txtZIP.ClientID%>") and document.getElementById("txtPostalcode").value both are used to retrive values from aspx page then pls tel me the diff betwen them quick pls
alex 0Posted Apr 27, 2007, 8:25 AM
hi document.getElementById("<%=txtWebURL.ClientID%>"). and document.getElementById("txtPostalcode").value both are used to retrive values from aspx then pls tel me the diff betwen them quick pls
Abdullah MukhtarPosted Apr 11, 2007, 10:03 AM
Hello Puran, thank you so much for the article. After I paste the code in the code behind file as you've instructed, i get this error: Name "btnSubmit" is not declared. Please how can I go about fixing this problem. Thanks
anju mishraPosted Mar 22, 2007, 2:34 AM
hi.. I have two radio buttons on my form n a Report button. On report button click i have 2 check which button is clicked and accordingly open th report. I m confused how 2 do this using document.getElementById on code behind to check. As it needs two clicks to open up the report.
karthick RamanujamPosted Mar 21, 2007, 2:27 PM
dood
Neeraj wadhwaniPosted Nov 21, 2006, 5:30 AM
where i have to put the script tag,in HEAD tag or in BODy tag,plz help me, i need it very badly..
Aditya KumarPosted Oct 10, 2006, 2:05 AM
Hello Sir, Thanx for your validataion with javascript article....that was realy helpfull.......Thanx. Sir, I want to know about ajax....Will you plz. put a realtime small example of ajax...Thanx and bye...
Vinay PateleditedPosted May 23, 2006, 5:42 AMEdited May 23, 2006, 5:44 AM
Thanks Mr purankaushal ur aritical is very helpfull , thanks a lots,please also give the idea how to use error provider in java script in web pages rather than message box for error message Regards, For Oriensoft Technologies Pvt. Ltd.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> Vinay Patel Software Programmer E-mail :- [email protected] Tel: +91-22-67111555 Fax: +91-22-67111666 24 x 7 Support & Sales Toll Free : 1-800-22-5171 (<?xml:namespace prefix = u1 /><u1:place u2:st="on"><u1:country-region u2:st="on">India</u1:country-region></u1:place> Only) Web : http://www.oriensoft.com<o:p></o:p> <o:p> </o:p>
Abdul SamiPosted May 20, 2006, 6:43 AM
Hi Puran, I would like to thank you for your article. It really helped me a lot in doing validation. I learned a lot from that article. I will contact you further if I have any doubts. Regards, Abdul Sami [email protected]