动态验证文本字段中的问题

时间:2013-07-16 06:40:20

标签: javascript

我想验证我的动态文本字段,我使用的是javascript。

在这一行我收到错误

<aui:form name="frmaddTravelDetails" id="frmaddTravelDetails" action="<%=processActionURL.toString()%>"  method="post" onSubmit="return validateAllInputBoxes(event)">

错误消息:

  

无法从函数或方法外部返回。

view.jsp的:

<script type="text/javascript" >


  function addRow(tableID) {

        //var uniqueId=1;

        var rowid = window.document.getElementById(1).id;
        alert("First row id-->"+rowid);
          var table = document.getElementById(tableID);

        var rowCount = table.rows.length;

        alert("value of rowCount"+rowCount);

        var row = table.insertRow(rowCount);

         var cell1 = row.insertCell(0);
         var element1 = document.createElement("input");
         element1.type = "checkbox";
         element1.name="selectDel[]"+rowCount;

           //cell1.appendChild(element1);

          var cell2 = row.insertCell(1) ;
          var element2 = document.createElement("input");
          element2.type = "text";
          //element2.name = "t02Traveldate[]" +rowCount;
          element2.name = "t02Traveldate" +rowCount;

           //cell2.appendChild(element2);

          var cell3 = row.insertCell(2);
          var element3 = document.createElement("input");
          element3.type = "text";
          element3.name = "t02Travelfrom"+rowCount;
          element3.value='';
          //cell3.appendChild(element3);

          var cell4 = row.insertCell(3);
          var element4 = document.createElement("input");
          element4.type = "text";
          element4.name = "t02Travelto"+rowCount;
          //cell4.appendChild(element4);

          var cell5 = row.insertCell(4);
          var element5 = document.createElement("input");
          element5.type = "text";
          element5.name = "t02Landmark"+rowCount;
          //cell5.appendChild(element5);

          var cell6 = row.insertCell(5);
          var element6 = document.createElement("input");
          element6.type = "text";
          element6.name = "t02Totalkm"+rowCount;
          //cell6.appendChild(element6); 

          var uniqueId;
        /* Start */
        for (var i=rowCount;rowCount>7;i++){

              uniqueId = i;
            //uniqueId=rowCount;
             alert("Value of unique id-->"+uniqueId);

             cell1.appendChild(element1);
             cell2.appendChild(element2);
             cell3.appendChild(element3);
             cell4.appendChild(element4);
             cell5.appendChild(element5);
             cell6.appendChild(element6);

             document.getElementById("hdn").value=uniqueId;
             document.frmaddTravelDetails.submit();


        }
          /* End */

      }   function send(tableID)  {       alert("call send function");
        var table = document.getElementById(tableID);
        var rowCount1 = table.rows.length;
        document.<portlet:namespace/>frmaddTravelDetails.<portlet:namespace/>hdn.value
= ""+rowCount1;
        document.<portlet:namespace/>frmaddTravelDetails.submit();

      }
    function deleteRow(tableID) {
          try {
          var table = document.getElementById(tableID);
          var rowCount = table.rows.length;

          for(var i=0; i<rowCount; i++) {
              var row = table.rows[i];
              var chkbox = row.cells[0].childNodes[0];
              if(null != chkbox && true == chkbox.checked) {


                  if(rowCount <= 8) {

                    alert("Cannot delete all the rows.");
                      break;
                  }
                  table.deleteRow(i);
                  rowCount--;
                  i--;
              }


          }
          }catch(e) {
              alert(e);
          }
      }    stopEvent = function(ffevent)  {
    var current_window = window;

    if(current_window.event) //window.event is IE, ffevent is FF
    {
       //IE
       current_window.event.cancelBubble = true; //this stops event propagation
       current_window.event.returnValue = false; //this prevents default (usually is what we want)
    }
    else
    {
       //Firefox
       ffevent.stopPropagation();
       ffevent.preventDefault();
    };  }

 function validateAllInputBoxes(ffevent)  {
    var inputs = document.getElementsByTagName('input');
    for(var i = 0; i < inputs.length; ++i)
       if(inputs[i].type === 'text')

          if(inputs[i].length==0) 
          {
             alert("form could not be sent one input text field is empty");
             stopEvent(ffevent);
          }  }


    </script>   </head> <body>   <portlet:actionURL name="processAction" var="processActionURL"/> 
     <%
         EMP_DETAIL empDetail=(EMP_DETAIL) request.getAttribute("empDetail");   %>
    <aui:form name="frmaddTravelDetails" id="frmaddTravelDetails" action="<%=processActionURL.toString()%>"  method="post" onSubmit="return validateAllInputBoxes(event)">
    <table id="dataTable" border="1">

      <tr>
      <th colspan="7" style="text-align:center;">Employee Travel Details</th></tr>
       <tr>
            <td>Employee ID</td>
            <td colspan="6">

            <aui:input name="t02Emplid" label="" type="text"  value='<%=empDetail.getM01Emplid() %>' /></td>
        </tr>
       <tr>
            <td>Employee Name</td>
            <td colspan="6"><aui:input name="t02Ename" label="" type="text" value='<%=empDetail.getM01Empname() %>' /></td>
       </tr>       
       <tr>
            <td>Vehicle Type</td>
            <td colspan="6">

                <aui:select   name="t02Vechiletype" label="" id="selectedVehicle">

                <c:forEach items="${empVehicleList}" var="empVehicle" >

                    <aui:option value="${empVehicle.m04Vehicle_Type}" >${empVehicle.m04Vehicle_Type}</aui:option>

                </c:forEach>

                </aui:select>           </td>       </tr>
       <tr>
            <td>Approver</td>
             <td colspan="6">
             <aui:select  name="t02Approver" label="" id="selectedApprover">


                <% List<String> listMangers = (List<String>) request.getAttribute("listManagers");

                Set<String> set = new HashSet<String>(listMangers);
                System.out.println("SIZE "+set.size());
                 for (String managerList : set){
                    %>

                    <aui:option value="<%=managerList%>" ><%=managerList%></aui:option>

            <%
                }           %>



             </aui:select>  
            </td>
       </tr>                    <tr>            <td>Vehicle Number</td>
            <td colspan="6">

                <aui:input name="t02Vechileno" id="t02Vechileno" type="text" maxlength="10" label="" value="" >
                 <aui:validator name="required"  errorMessage="this-field-is-required"/> 
                 <aui:validator name="alphanum" />
                </aui:input> 
                </td>

        </tr>
        <tr>
            <td>Select</td>

            <td>Date(dd/mm/yyyy)</td>
            <td>From</td>
            <td>To</td>
            <td>Landmark</td>
            <td colspan="6">Total KM</td>
      </tr>      
       <tr id=1>
            <td><aui:input name="selectDel" type="checkbox" label=""/></TD>

            <td>
                 <aui:input name="t02Traveldate" type="text" value="" label="">
                <aui:validator name="date" />   
                  </aui:input>

            </td>
            <td>
                <aui:input name="t02Travelfrom" type="text" label="" maxlength="15" value="">

                    <aui:validator name="required"  errorMessage="this-field-is-required"/>
                    <aui:validator name="alpha" />

                 </aui:input>
            </td>
            <td>
                <aui:input name="t02Travelto" type="text" label=""  maxlength="15" value="" >

                    <aui:validator name="required"  errorMessage="this-field-is-required"/>
                    <aui:validator name="alpha" />

                </aui:input>
            </td>     
            <td>
                <aui:input name="t02Landmark" type="text" label=""  maxlength="15" value="">

                    <aui:validator name="required"  errorMessage="this-field-is-required"/>
                    <aui:validator name="alpha" />

                </aui:input>

            </td>
            <td>
                <aui:input name="t02Totalkm" type="text" label="" maxlength="7" value="">

                    <aui:validator name="required"  errorMessage="this-field-is-required"/>
                    <aui:validator name="digits" />

                </aui:input>

                 <input type="hidden" name="hdn" id="hdn" />


            </td>


       </tr>

    </table>

    <table id="dataTable1" border="1">
        <tr>
            <td colspan="7" style="text-align:center;"><aui:button name="submitAction" type="submit" value="Apply" /></td>
            <td><aui:button name="addRow" type="button" value="Add Row" onclick="addRow('dataTable')" /></td>

            <td><aui:button name="deleteRow" type="button" value="Delete Row" onclick="deleteRow('dataTable')" /></td>

        </tr>
    </table>
    </aui:form>   </body> </html>

1 个答案:

答案 0 :(得分:0)

通常情况下,如果onSubmit="return myFunction()"返回myFunction,则false取消事件,如果返回true则继续。 但是你的函数什么也没有返回,我认为使用"return MyFunction()"是不恰当的。

此外,在validateAllInputBoxes您测试if(inputs[i].length==0)但输入文本对象 没有长度属性(我知道)你应该尝试测试inputs[i].value == null or inputs[i].value.length==0