Click here to Skip to main content
15,748,930 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem here I need to do validation in my form I have a Subject field, To field and Body field all these are text boxes. I can do validation individually by writing validation code for each text box and each time I press the save button it will show that particular field is required. But I need to validate it on Save Button rather than validating each text box, I want that when I press Save Button on my page if there is any field left blank or data is wrong then it show error.

Any help will be appreciated.

Please reply to this post ASAP.

<script lang="javascript" type="text/javascript">
function Validation() {
           var name = document.getElementById('<%=txtFirstName.ClientID %>');
           var fathername = document.getElementById('<%=txtFatherName.ClientID %>');
           var address = document.getElementById('<%=txtAddress.ClientID %>');

           if (name.value == "") {
               alert("Please Enter First Name");
               return false;
           if (fathername .value == "") {
               alert("Please Enter father Name");
               fathername .focus();
               return false;
           if (address.value == "") {
               alert("Please Enter Address");
               return false;


and call function as below

<asp:button id="btn" runat="server" OnClientClick="javascript:return Validation()" />

If you are using html button then you need to call the javascript function in onclick event.
Share this answer
Write a javascript function to validate all the controls and call that function in OnClientClick of Submit button
Share this answer
@Simple.V "Is it I need to call OnClientClick or onsubmit"
Share this answer
sravani.v 26-Sep-11 7:35am    
R u using Button or Html Button
Narayan1.Vikas 26-Sep-11 12:27pm    
@Simple.V "HTML"
sravani.v 27-Sep-11 0:35am    
Then u need to call it in OnClick

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900