I have a field in jsp page that has some textboxes. but the texboxes are dynamically created depending on the no of rows in a table.
When i click the update button in my jsp page it has to get the values in the text boxes and get the total.
My java script first checks if the field is valid i.e if there are any textbox in the field. If there are no fields then it set a variable as false. but if there are any fields it takes the length of the field to determine the no of textboxes and iterates through each text box to add all the value. The javascript works fine if there are no textboxs or if there are more then one textbox..
But when i have only one textbox in the field the field.length command returns me the value 'undefined'.
Below is the part of my java script code.
Expand|Select|Wrap|Line Numbers
- function validateSupplierCost()
- {
- var bValid = false;
- if(document.dcisForm.txtTotalCompEstimate){
- bValid = true;}
- var totalCompEstimate = document.dcisForm.txtTotalCompEstimate;
- var supplierCostResponseGrp = document.dcisForm.rbCost;
- var respInvest = document.dcisForm.C5;
- var respDieMod = document.dcisForm.C4;
- var respPartMod = document.dcisForm.C2;
- var respOther = document.dcisForm.C1;
- var bResponseType = false;
- if(respInvest.checked == true || respDieMod.checked == true || respPartMod.checked == true|| respOther.checked == true) {
- bResponseType = true;
- }
- var bSupplierResponse = false;
- for (var i =0; i< supplierCostResponseGrp.length; i++)
- {
- if (supplierCostResponseGrp[i].value == "Y") {
- if (supplierCostResponseGrp[i].checked == true) {
- bSupplierResponse = true;
- }
- }
- }
- if(bSupplierResponse == true && bResponseType == true) {
- if(bValid == true){
- var noOfFields = totalCompEstimate.length;
- var totalCost = 0;
- var cost = 0;
- for(i=0; i< noOfFields; i++)
- {
- cost = totalCompEstimate[i].value;
- totalCost = totalCost + cost;
- }
- if(totalCost > 0){
- return true;
- } else {
- return false;
- }
- }else {
- var compEstimate = document.dcisForm.txtCompEstimate;
- if(compEstimate.value >0){
- return true;
- } else {
- return false;}
- }
- } else {
- return true;
- }
- }