Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
function Validate()
        {
            if(document.getElementById("<%= ddlClient.ClientID %>").value==0)
	         {
		        document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		        document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Select Client! ";		    
		         return false;
	         }
	         if(document.getElementById("<%= txtname.ClientID %>").value=="")
	         {
		        document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		        document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Enter Job Name! ";	
		        document.getElementById("<%= txtname.ClientID %>").focus();  	    
		         return false;
	         }
	         if(document.getElementById("<%= txtqty.ClientID %>").value=="")
	         {
	            if(document.getElementById("<%= txtamount.ClientID %>").value=="")
	            {
		            document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		            document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Enter Quantity! ";		
		            document.getElementById("<%= txtqty.ClientID %>").focus();   
		        }
		         return false;
	         }
	         
	         if(document.getElementById("<%= ddlratetype.ClientID %>").value==0)
	         {
		        document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		        document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Select Rate Type! ";		    
		         return false;
	         }
	         if(document.getElementById("<%= ddlratetype.ClientID %>").value!="Rate Per LOT")
	         {
	            if(document.getElementById("<%= txtrate.ClientID %>").value=="")
	            {
		            document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		            document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Enter Rate! ! ";		    
		            return false;
		        }
	         }
	          
	         if(document.getElementById("<%= txtamount.ClientID %>").value=="")
	         {
		        document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		        document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Enter Amount! ";	
		         document.getElementById("<%= txtamount.ClientID %>").focus(); 	    
		         return false;
	         }
	         if(document.getElementById("<%= txttax.ClientID %>").value=="")
	         {
		        document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		        document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Enter Tax! ";	
		         document.getElementById("<%= txttax.ClientID %>").focus(); 	    
		         return false;
	         }
	           if(document.getElementById("<%= ddltaxtype.ClientID %>").value==0)
	         {
		        document.getElementById("<%=lblErrormsg.ClientID%>").className="error";
		        document.getElementById("<%=lblErrormsg.ClientID%>").innerHTML="Select Tax Type! ";		    
		         return false;
	         }
	         return true;
        }
Posted
Updated 2-Jan-11 19:51pm
v4
Comments
T.Saravanann 3-Jan-11 1:43am    
Hi what is this...Are you show your code...

1 solution

OK, don't return true or false, return some useful information on validation failure.

For example, return your inner HTML instead or a class combining inner HTML text and className. It will help you to avoid repeated assignment if every "if" block, all those repeated getElementById, etc. The principle is called Don't Repeat Yourself (D.R.Y., http://en.wikipedia.org/wiki/DRY[^]), one of the most basic and important. As a result, your code will be shorter and of much better supportability.

The properties of the elements showing innerHTML with the style className will be assigned only once in some function calling your function Validate.

That's it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jan-11 2:06am    
You did not really ask any question by the time I decided to give this advice.
I just thought you really need this improvement. Therefore, nobody should blame me answering wrong question.

Are you going to ask any question, anyway?

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