Click here to Skip to main content
14,427,893 members
Rate this:
Please Sign up or sign in to vote.
See more:
    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 2: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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can check the CP link which handles different javascript validations.

JavaScript form validations made easy[^]

Hope this helps
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

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");
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 5

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 :)
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100