Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
JavaScript
    var Pname = document.getElementById('<%=txtdate.ClientID %>').value;
            if (Pname == " " ) {
                alert("Please Enter Date.");
                return false;
            }
  var vechileno = document.getElementById('<%=txtVehicleNo.ClientID %>').value;
            if (vechileno == " " ) {
                alert("Please Enter  Vehicle Number.");
                return false;
            }

           var ddlItemtype = document.getElementById('<%=ddlItemtype.ClientID%>');
            var ddlitem = ddlItemtype.options[ddlItemtype.selectedIndex].value;
            if (ddlitem == 0) {
                alert("Please Select Item.");
                return false;
            }
            var ddltrader = document.getElementById('<%=ddltradername.ClientID%>');
            var ddlproductype1 = ddltrader.options[ddltrader.selectedIndex].value;
            if (ddlproductype1 == 0) {
                alert(" Please Select Trader Name.");
                return false;
            }
}


On button click I am calling this function in javascript
I have manytextboxes and dropdown list like this . I want to use for each loop and display the error.

I am writing the code all the time like this. And the length of coding increasing. Is there any optimize way to reduce the code.
Posted
Updated 3-Oct-13 1:48am
v3
Comments
ZurdoDev 3-Oct-13 7:42am    
OK, what's the problem?
Bhagavan Raju M 3-Oct-13 7:44am    
I am writing the code all the time like this. And the length of coding increasing. Is there any optimize way to reduce the code.
ZurdoDev 3-Oct-13 7:45am    
Yes, you can use jquery. I would google for "jquery validate all dropdowns" or something like that. jquery has an .each function which will iterate through all of whatever you tell it to find.
Bhagavan Raju M 3-Oct-13 7:47am    
Is it possible with java script
ZurdoDev 3-Oct-13 7:56am    
jquery is javascript. It's a javascript library and you use it by including the jquery.js file. It will save you tons of time.

 
Share this answer
 
Comments
Bhagavan Raju M 3-Oct-13 7:59am    
@thatraja I am using web application. Can you provide me the similar link for web application
thatraja 3-Oct-13 8:06am    
:( that's for web application.
You can check the CP link which handles different javascript validations.

JavaScript form validations made easy[^]

Hope this helps
 
Share this answer
 
v2
I suggest you to use the jquery validation engine given by Github. It will also provide you custom validations with elegant error messages.

You will find all documentation and source from the given link:
https://github.com/posabsolute/jQuery-Validation-Engine[^].

You can experience the demo for the same on link:
http://www.position-relative.net/creation/formValidator/demos/demoValidators.html[^].

I hope this will help you.
 
Share this answer
 
You can get Elements using Tag Name by JavaScript and loop through the elements one by one and check their value.

For input fields - document.getElementsByTagName("input");
For DropdownLists - document.getElementsByTagName("select");
 
Share this answer
 
v2
This may save your time
Your code doesn't seems to be lengthy at all i am giving you another approaches

http://jqueryvalidation.org/[^]

http://jqueryvalidation.org/documentation/[^]

Jquery is a java script library although this links give you a right ideas and ways to implement validations i hope this i will help you :)
 
Share this answer
 
 
Share this answer
 

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