代码之家  ›  专栏  ›  技术社区  ›  Pinu

ASP.NET MVC验证

  •  0
  • Pinu  · 技术社区  · 14 年前

    我需要验证高级搜索表单,但是它有数据要发送给2个不同的表人员和文档,所以我不能使用数据注释。我需要验证名称是否仅为字符,帐号和金额是否为数字。如何验证这些字段?

    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
        <center>
            <img src="../../Content/images/DocuVault_Logo.png" alt="DocuVault" />
            <%= Html.ValidationSummary() %>
            <% using (Html.BeginForm("QuickSearch", "Search"))
            { %>
                <div id="div_QuickSearch">
                    <table>
                        <tr>
                            <td colspan="2">
                            <%= Html.TextBox("search", "",  new { style = "width: 300px" })%>
                            <input type="submit" value="Search" />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                            <%= Html.ValidationMessage("search") %>
                            </td>
                        </tr>
                        <tr>
                            <td><a href="#" id="link_advanced">Advanced Search</a></td>
                        </tr>
                    </table>
                    <br />
                </div>
            <% } %>
            <ul>
            </ul>
            </div>
            <% using (Html.BeginForm("AdvancedSearch", "Search")) 
            { %>
                <div id="div_AdvancedSearch" style="display: none; width: 420px; padding: 10px;">
                    People
                    <table style="border: solid 1px black; padding: 5px; width: 400px;">
                        <tr>
                            <td>Name:</td>
                            <td align="right">
                            <%= Html.TextBox("searchName") %>
                            <%= Html.ValidationMessage("searchName")%>
                            </td>
                        </tr>
                        <tr>
                            <td>Address:</td>
                            <td align="right">
                            <%= Html.TextBox("searchAddress") %>
                            <%= Html.ValidationMessage("searchAddress")%>
                            </td>
                        </tr>
                        <tr>
                            <td>Account Number:</td>
                            <td align="right">
                            <%= Html.TextBox("searchAccountNumber") %>
                            <%= Html.ValidationMessage("searchAccountNumber")%>
                            </td>
                        </tr>
                    </table>
                    <br />
                    Documents
                    <table style="border: solid 1px black; padding: 5px; width: 400px;">
                        <tr>
                            <td>Invoice:</td>
                            <td align="right">
                            <%= Html.TextBox("searchInvoice") %>
                            <%= Html.ValidationMessage("searchInvoice")%>
                            </td>
                        </tr>
                        <tr>
                            <td>Amount:</td>
                            <td align="right">
                            <%= Html.TextBox("searchAmount") %>
                            <%= Html.ValidationMessage("searchAmount")%>
                            </td>
                        </tr>
                        <tr>
                            <td>Job:</td>
                            <td align="right">
                            <%= Html.TextBox("searchJob") %>
                            <%= Html.ValidationMessage("searchJob")%>
                            </td>
                        </tr>
                        <tr>
                            <td>SDI:</td>
                            <td align="right">
                            <%= Html.TextBox("searchSDI") %>
                            <%= Html.ValidationMessage("searchSDI")%>
                            </td>
                        </tr>
                        <tr>
                            <td>Date:</td>
                            <td align="right">
                            <%= Html.TextBox("searchDateBegin", "", new { style = "width: 88px" })%>
                            to
                            <%= Html.TextBox("searchDateEnd", "", new { style = "width: 88px" })%>
                            <%= Html.ValidationMessage("searchDate")%>
                            </td>
                        </tr>
                    </table>
                    <br />
                    <div style="width: 100%;">
                        <span style="float: left; margin-left: 10px;"><a href="#" id="link_quick">QuickSearch</a></span>
                        <span style="float: right; margin-right: 10px;"><input type="submit" value="Advanced Search" /></span>
                    </div>
                </div>
            <% } %>
        </center>
        </asp:Content>
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   Daniel Dyson    14 年前

    尝试 FluentValidation . 它是一个非常简单和强大的验证框架,能够根据规则验证类。它还可以用于生成客户端jquery验证器

        2
  •  0
  •   Pinu    14 年前

    我使用jquery插件验证,并使用它执行验证。因为我想保持简单。