使用javascript进行Asp-Net表单验证

时间:2017-01-02 13:15:44

标签: javascript jquery asp.net

我正在尝试使用JavaScript,问题或错误进行验证当我检查它给出的条件返回true; 时,我从控件中获取所有值(null或&# 34;")我检查在浏览器调试器中我希望在我让所有字段为空时获得警报,任何帮助都表示赞赏。



 <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;
        }
&#13;
      <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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您需要从条件

中删除
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;
            }
        }

答案 1 :(得分: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" />