由于嵌套if else,无法在javascript中正确执行表单验证

时间:2018-04-13 08:42:19

标签: javascript if-statement nested

如果填写了所有必填字段,我想保存表单

我无法正确执行表单验证。 ProjName 之后的代码工作正常,但在该代码表单显示警告消息之前,即使在返回false之后也会保存信息。请帮助。 以下是我的代码:

    function validateRequiredFields()
        {
               if($("input[name='upload1']:checked").val()=="Yes")  
                {               
                  if($("input[name='Beneficiary']:checked").val()=="Indiviual")  
                   {
                     if($("#titlename option:selected").text() =="--Select--")
                     {
                                alert("Please Enter title");
                                $("#titlename").focus();
                                return false;
                     }

                    if($('#IBfirst_name').val()=="")
                       {
                                $("#IBfirst_name").focus();
                                alert("Please Enter FirstName");
                                return false;
                       }
                       if($('#IBLast_name').val() == "")
                       {
                                $("#IBLast_name").focus();
                                alert("Please Enter LastName");
                                return false;
                       }
                     if($("input[name='gender']:checked").val()=="")
                       {
                                $("#gender").focus();
                                alert("Please Select Gender");
                                return false;
                       }
                    if($('#IBDob').val()=="")
                       {
                                $('#IBDob').focus();
                                alert("Please Select Date Of Birth");
                                return false

                       }
                     if($('#add1').val()=="")
                       {
                                $("#add1").focus();
                                alert("Please Enter Address");
                                return false;
                       }
                     if($("#stcodeCoal1 option:selected").text() =="--Select State--")
                       {
                                $("#stcodeCoal1").focus();
                                alert("Please Enter State");
                                return false;
                       }
                    if($("#dtcodeCoal1 option:selected").text()=="--Select District--")
                       {
                                $("#dtcodeCoal1").focus();
                                alert("Please Enter District");
                                return false;
                       }
                     if($('#ProjPincodeIB').val()=="")
                       {
                                $("#ProjPincodeIB").focus();
                                alert("Please Enter PinCode");
                                return false;
                       }
                    if ($('#ProjPincodeIB').val().length != 6) {
                                $("#ProjPincodeIB").focus();
                                alert("Please Enter Valid Pin Code");
                                return false;

                       } 
                     if($('#IBAdharCardNo').val()=="")
                       {
                                $("#IBAdharCardNo").focus();
                                alert("Please Enter Aadhar Card No");
                                return false;
                       }
                    if ($('#IBAdharCardNo').val().length != 12)
                        {
                        $("#IBAdharCardNo").focus();
                                alert("Please Enter Valid Aadhar Card No");
                                return false;

                        } 
                     if($('#IBBenId').val()=="")
                       {
                                $("#IBBenId").focus();
                                alert("Please Enter Beneficiary ID");
                                return false;
                       }
                        else
                        {
                            alert("individual else");
                        }


                } //end individual
                else if($("input[name='Beneficiary']:checked").val()=="Group")  
                    {                    
                      if($("#grpbVillages option:selected").text() =="--Select--")
                         {
                                $("#grpbVillages").focus();
                                alert("Please Select Villages");
                                return false;
                         }                              
                      if($('#gbNoOfBen').val()=="")
                         {
                                $("#gbNoOfBen").focus();
                                alert("Please Enter Number of Beneficiaries");
                                return false;
                         }
                        else
                        {
                            alert("Beneficiary else");
                        }


                     }  // end if group beneficiary  */





                }      //end if yes/no   




                    //Infrastructure and others
                    else if($("input[name='upload1']:checked").val()=="No")  
                        { 
                              if($("input[name='InfraOthers']:checked"))  
                              {
                                            if($("#stcodeCoal2 option:selected").text() =="--Select State--")
                                             {
                                              $("#stcodeCoal2").focus();
                                             alert("Please Enter State");
                                             return false;
                                             }
                                            else
                                              if($("#dtcodeCoal2 option:selected").text()=="--Select District--")
                                             {
                                             $("#dtcodeCoal2").focus();
                                             alert("Please Enter District");
                                             return false;
                                             }
                                               else
                                              if($('#InfraPinCode').val()=="")
                                                {
                                               $("#InfraPinCode").focus();
                                                alert("Please Enter PinCode");
                                                 return false;
                                                }
                                             else if ($('#InfraPinCode').val().length != 6)
                                                 {
                                               $("#InfraPinCode").focus();
                                             alert("Please Enter Valid Pin Code");
                                           return false;

                                             } 
                                           else if($('#infraVillage').val()=="")
                                                {
                                               $("#infraVillage").focus();
                                                alert("Please Enter Village");
                                                 return false;
                                                }
                                               else 
                                                if($('#InfraNoOfBen').val()=="")
                                                 {
                                                 $("#InfraNoOfBen").focus();
                                                 alert("Please Enter Number of Beneficiaries");
                                                 return false;
                                                  }


                              }
                        }


                else if($("#ProjName").val()=="")
                {
                                $("#ProjName").focus();
                                alert("Please Select ProjectName");
                                return false;
                }
                else if($("#VendorName option:selected").text()=="--Select--"){
                $("#VendorName").focus();
                               alert("Please Enter Vendor");
                               return false;
                }       
                else if($('#ContractPeriodFromDate').val()=="")
                {
                $("#ContractPeriodFromDate").focus();
                              alert("Please Enter Contract Period From Date");
                              return false;
                }
                else if($('#ContractPeriodToDate').val()=="")                   
                {
                $("#ContractPeriodToDate").focus();
                               alert("Please Enter Contract Period To Date");
                               return false;
                }  
                else
                if($("#stcodeCoal option:selected").text() =="--Select State--")
                {
                                $("#stcodeCoal").focus();
                                alert("Please Enter State");
                                return false;
                }
                else
                if($("#dtcodeCoal option:selected").text()=="--Select District--")
                {
                                $("#dtcodeCoal").focus();
                                alert("Please Enter District");
                                return false;
                }
                else
                if($("#ProjHighPriority option:selected").text()=="--Select--")
                {
                                $("#ProjHighPriority").focus();
                                alert("Please Enter High Priority");
                                return false;
                }
                else if($("#ddProjectStatus option:selected").text()=="--Select--"){
                $("#ddProjectStatus").focus();
                             alert("Please Enter Project Status");
                             return false;
                }

                else
                if($('#ProjPincode').val()=="")
                {
                                $("#ProjPincode").focus();
                                alert("Please Enter PinCode");
                                return false;
                }
                else if ($('#ProjPincode').val().length != 6) {
                $("#ProjPincode").focus();
                                alert("Please Enter Valid Pin Code");
                                return false;

                } 
                else if ($('#ProjSanctionedOrderDate').val()=="") {
                $("#ProjSanctionedOrderDate").focus();
                                alert("Please Enter Sanctioned Order Date");
                                return false;

                }
                else if ($('#ProjSanctionedAmountDate').val()=="") {
                $("#ProjSanctionedAmountDate").focus();
                                alert("Please Enter Sanctioned Amount Date");
                                return false;

                } 

                else if ($('#ddActualStartDate').val()=="") {
                $("#ddActualStartDate").focus();
                                alert("Please Enter Actual Start Date");
                                return false;

                }
                else if ($('#ddActualDateofCompletion').val()=="") {
                $("#ddActualDateofCompletion").focus();
                                alert("Please Enter Actual Date of Completion");
                                return false;

                }
                else if ($('#ddTargetCompletionDate').val()=="") {
                $("#ddTargetCompletionDate").focus();
                                alert("Please Enter Target Completion Date");
                                return false;

                }
                else if ($('#ddScrappedTerminatedDate').val()=="") {
                $("#ddScrappedTerminatedDate").focus();
                                alert("Please Enter Scrapped Terminated Date");
                                return false;

                }                               
                // section wise validations
                 else
                {
                     return true;
                }   
         } 
      //function validateRequiredFields end

通过调用formave函数保存我的表单。

      function formSave() { 
       validateRequiredFields();
      // csom code to save data to Sharepoint list
       }

要提交的HTML代码:

      <input type="button" " value="Submit" id="NewSaveItem" 
      onclick="formSave()">

0 个答案:

没有答案