为过滤器选择要求至少一个字段的Jquery验证?

时间:2011-12-06 13:44:08

标签: jquery asp.net-mvc-3 validation

有人可以给我一个Jquery示例来强制执行至少一个字段被选中。 我的问题如何检查复选框和下拉列表(默认空字符串)等项目? 注意:使用ASP.NET MVC3 C#

我在视图中的代码:

 <fieldset  id="fieldset1" class="coolfieldset">
        <legend>Search/Contacts</legend>
        <div class="div-table">
        <div class="div-table-row">
            <div class="div-table-col">Reg Date:</div>
            <div class="div-table-col"><input id="regDateFrom" class="datepicker" name="regDateFrom" value="@regDateFrom" type="text" /> to <input id="regDateEnd" class="datepicker" value="@regDateEnd" name="regDateEnd" type="text" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col">Profile Mod Date:</div>
            <div class="div-table-col"><input type="text" id="profileModDateFrom" class="datepicker" value="@profileModDateFrom"  name="profileModDateFrom" /> to <input id="profileModDateEnd" class="datepicker" value="@profileModDateEnd" name="profileModDateEnd" type="text" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col">Last Name:</div>
            <div class="div-table-col"><input type="text" class="required_group"  id="lastName" name="lastName" value="@lastName" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col"> First Name:</div>
            <div class="div-table-col"><input type="text" class="required_group"  id="firstName" name="firstName" value="@firstName"  /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col"> Company:</div>
            <div class="div-table-col"> <input type="text" class="required_group"  id="companyname" name="companyname" value="@companyname" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col">Publication:</div>
            <div class="div-table-col"> 
            @Html.DropDownList(
            "publication", 
                new SelectList(
                    ViewData["pubs"] as System.Collections.IEnumerable,
                    "PublicationID",
                    "PublicationFullName",
                    publication
                ),""
            )
            </div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col"> Country:</div>
            <div class="div-table-col"> 
             @Html.DropDownList(
            "country", 
                new SelectList(
               ViewData["countries"] as System.Collections.IEnumerable,
                   "Country",
                   "Country",
                    country
                ),""
            )
            </div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col">Client Only:</div>
            <div class="div-table-col"> <input type="checkbox" id="cisonly" name="cisonly" value="1" @bChecked />
        </div>
          <div class="div-table-row">
            <div class="div-table-col"><input id="search" name="search" type="submit" value="Search" /></div>
            <div class="div-table-col"></div>
        </div>
        </div>      
    </fieldset>

1 个答案:

答案 0 :(得分:1)

要检查是否至少选中了复选框,您可以使用:

 if($('input:checkbox:checked').length > 0){
       //at least one checkbox checked

单选按钮

相同
 if($('input:radio:checked').length > 0){
       //at least one checkbox checked