tags:

views:

48

answers:

2

I need to validate the Advanced search form , but it has data to be sent to 2 different table people and documents so i am unable to use data annotations. I need to validate the Name to be characters only, account number & amount to be a number.How can i validate these fields?

<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>
A: 

Try FluentValidation. It is a very simple and powerful validation framework that is able to validate your classes based on rules. It can also be used to generate ClientSide JQuery Validators

Daniel Dyson
My major issue , I am not relating my textboxes with my classes and also i have data from two different tables in one form?
Pinu
Fluent Validation works against C# objects, so you would be best advised to use it within the model, although it is able to generate appropriate Clientside validation. The other option might be to use standard ASP.NET validation controls and have a different ValidationGroup for each form? Just an idea
Daniel Dyson
Pinu, but your textboxes should relate at least to you Model being posted to the controller, right? So you can do validation on your Model classes using FluentValidation or the MVC2 out-of-the-box validation.
Roberto Hernandez
@Daniel , "The other option might be to use standard ASP.NET validation controls and have a different ValidationGroup for each form?" This sounds do able , do you have any example on how i could do this? Thanks so much for your help
Pinu
A: 

I used the jquery plugin validate and using that I performed the validations. As I wanted to keep things simple.

Pinu