2017-01-02 56 views
0

我試圖讓驗證使用JavaScript,問題或錯誤我當我檢查,如果條件它給迴歸真實的形式驗證;,我從控件(null或「」)得到所有的值我檢查,在瀏覽器調試我想要得到警報,當我讓所有字段爲空,任何幫助表示讚賞。ASP的網絡使用JavaScript

<script type="text/javascript"> 
 
     function ValidateProjectForm() { 
 

 
      var TxtProjectName, ddlDistrict, txtArea, txtPaymentSystem, ddlPurpose, ddlTypes, txtDate, txtClassification, txtlatitude, txtlongitude; 
 

 
      TxtProjectName = document.getElementById("TxtProjectName").value; 
 
      ddlDistrict = document.getElementById("ddlDistrict").value; 
 
      txtArea = document.getElementById("txtArea").value; 
 
      txtPaymentSystem = document.getElementById("txtPaymentSystem").value; 
 
      ddlPurpose = document.getElementById("ddlPurpose").value; 
 
      ddlTypes = document.getElementById("ddlTypes").value; 
 
      txtDate = document.getElementById("txtDate").value; 
 
      txtClassification = document.getElementById("txtClassification").value; 
 
      txtlatitude = document.getElementById("txtlatitude").value; 
 
      txtlongitude = document.getElementById("txtlongitude").value; 
 

 
      if (TxtProjectName == "" && ddlDistrict == 0 && txtArea == "" && txtPaymentSystem == "" && ddlPurpose == 0 && ddlTypes == 0 && txtDate == "", txtClassification = "", txtlatitude = "", txtlongitude = "") 
 
      { 
 

 
       alert("Enter All Fields"); 
 

 
       return false; 
 

 
      } 
 
      return true; 
 
     }
 <table border="0" width="100%" cellpadding="0" cellspacing="0"> 
 
          <tr valign="top"> 
 
           <td> 
 
            <!-- start id-form --> 
 
            <table border="0" cellpadding="8" cellspacing="8" id="id-form"> 
 
             <tr> 
 
              <th valign="top">الموقع:</th> 
 
              <td> 
 
               <label> 
 
                <asp:DropDownList class="styledselect_form_1" Width="135" Height="25" EnableViewState="true" ID="ddlDistrict" runat="server"> 
 
                </asp:DropDownList> 
 
               </label> 
 
              </td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">اسم المشروع:</th> 
 
              <td> 
 
               <input id="TxtProjectName" runat="server" type="text" class="textbox" /></td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">المساحات:</th> 
 
              <td> 
 
               <input id="txtArea" runat="server" type="text" class="textbox" /></td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">نظام السداد:</th> 
 
              <td> 
 
               <input id="txtPaymentSystem" runat="server" type="text" class="textbox" /></td> 
 
              <td></td> 
 
             </tr> 
 

 
             <tr> 
 
              <th valign="top">الغرض:</th> 
 
              <td> 
 
               <label> 
 

 
                <asp:DropDownList class="styledselect_form_1" Width="135" Height="25" EnableViewState="true" ID="ddlPurpose" runat="server"> 
 
                </asp:DropDownList> 
 
               </label> 
 
              </td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">النوع:</th> 
 
              <td> 
 
               <label> 
 
                <asp:DropDownList class="styledselect_form_1" EnableViewState="true" Width="135" Height="25" ID="ddlTypes" runat="server"> 
 
                </asp:DropDownList> 
 
               </label> 
 
              </td> 
 
              <td></td> 
 
             </tr> 
 

 
             <tr> 
 
              <th valign="top">تاريخ الاستلام:</th> 
 
              <td class="noheight"> 
 

 
               <asp:TextBox ID="txtDate" runat="server" class="textbox"></asp:TextBox> 
 
               <asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" /> 
 
               <cc1:CalendarExtender PopupButtonID="imgPopup" ID="CalendarExtender1" runat="server" TargetControlID="txtDate" 
 
                Format="MM/dd/yyyy" Enabled="true" /> 
 
              </td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">تصنيف العقار:</th> 
 
              <td> 
 
               <textarea id="txtClassification" runat="server" style="width: 378px; height: 121px;" class="textbox"></textarea></td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">خط الطول:</th> 
 
              <td> 
 
               <input id="txtlatitude" runat="server" type="text" class="textbox" /></td> 
 
              <td></td> 
 
             </tr> 
 
             <tr> 
 
              <th valign="top">خط العرض:</th> 
 
              <td> 
 
               <input id="txtlongitude" runat="server" type="text" class="textbox" /></td> 
 
              <td></td> 
 
             </tr> 
 

 
             <tr> 
 
              <th>صور تفاصيل المشروع الاعلى:</th> 
 
              <td> 
 
               <asp:FileUpload ID="FileProjectDetailsUp" EnableViewState="true" class="file_1" dir="rtl" runat="server" /> 
 
              </td> 
 
              <td> 
 
               <asp:Button ID="btndisplayUploadedImages" Width="120px" Height="30px" class="button" Text="تحميل" OnClick="FileProjectDetailsUp_Click" runat="server" /></td> 
 
              <td> 
 
               <asp:Repeater runat="server" ID="RepaterImages"> 
 
                <ItemTemplate> 
 
                 <img id="Image" runat="server" data-u="image" style="width: 120px;" src='<%#Container.DataItem %>' /> 
 
                </ItemTemplate> 
 
               </asp:Repeater> 
 
              </td> 
 
             </tr> 
 
             <tr> 
 
              <th>صور تفاصيل المشروع الاسفل:</th> 
 
              <td> 
 
               <asp:FileUpload ID="FileProjectDetailsDown" EnableViewState="true" class="file_1" dir="rtl" runat="server" /> 
 
              </td> 
 
              <td> 
 
               <asp:Button ID="btn_uploadedImg" Width="120px" Height="30px" class="button" Text="تحميل" OnClick="FileProjectDetailsDown_Click" runat="server" /></td> 
 
              <td> 
 
               <asp:Repeater runat="server" ID="RepeaterImg"> 
 
                <ItemTemplate> 
 
                 <img id="Image" runat="server" data-u="image" style="width: 120px;" src='<%#Container.DataItem %>' /> 
 
                </ItemTemplate> 
 
               </asp:Repeater> 
 
              </td> 
 
             </tr> 
 
             <tr> 
 
              <th>صورة المشروع:</th> 
 
              <td> 
 
               <asp:FileUpload ID="FileProjectUpload" EnableViewState="true" Enabled="true" class="file_1" dir="rtl" runat="server" /></td> 
 
              <asp:Label ID="StatusLabel" runat="server" /> 
 
             </tr> 
 
             <tr> 
 
              <th>&nbsp;</th> 
 
              <td valign="top"> 
 
               <br /> 
 
               <br /> 
 
               <asp:Button ID="btn_AddNewProject" OnClick="btn_AddNewProject_Click" OnClientClick="ValidateProjectForm();" Width="120px" Height="30px" class="button" Text="اضافة مشروع جديد" runat="server" /><br /> 
 
               <br /> 
 
               <br /> 
 
              </td> 
 
              <td></td> 
 
             </tr> 
 
            </table> 
 
            <!-- end id-form --> 
 

 
           </td> 
 
           <td></td> 
 
          </tr> 
 
          <tr> 
 
           <td> 
 
            <img src="images/shared/blank.gif" width="695" height="1" alt="blank" /></td> 
 
           <td></td> 
 
          </tr> 
 
         </table>

回答

0

你需要從if條件

function ValidateProjectForm() { 

      var TxtProjectName, ddlDistrict, txtArea, txtPaymentSystem, ddlPurpose, ddlTypes, txtDate, txtClassification, txtlatitude, txtlongitude; 

      TxtProjectName = document.getElementById("TxtProjectName").value; 
      ddlDistrict = document.getElementById("ddlDistrict").value; 
      txtArea = document.getElementById("txtArea").value; 
      txtPaymentSystem = document.getElementById("txtPaymentSystem").value; 
      ddlPurpose = document.getElementById("ddlPurpose").value; 
      ddlTypes = document.getElementById("ddlTypes").value; 
      txtDate = document.getElementById("txtDate").value; 
      txtClassification = document.getElementById("txtClassification").value; 
      txtlatitude = document.getElementById("txtlatitude").value; 
      txtlongitude = document.getElementById("txtlongitude").value; 

      if (TxtProjectName == "" && ddlDistrict == 0 && txtArea == "" && txtPaymentSystem == "" && ddlPurpose == 0 && ddlTypes == 0 && txtDate == "" && txtClassification = "" && txtlatitude = "" && txtlongitude = "") 
      {  
       alert("Enter All Fields");  
       return false;  
      } 
      else{ 
       return true; 
      } 
     } 
+0

是的,這是正確的 – hashim

0

我可以建議asp.net Validation Controls刪除。

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
<br /> 
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required field" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> 
<br /> 
<asp:Button ID="Button1" runat="server" Text="Button" />