2

我有一个带有几个文本框的剃刀视图。

我想这样做,以便您选择一个文本框或其他 3 个文本框,除非您在其中至少一个选项中输入文本,否则您无法提交...即您可以输入社会安全号码,或者您可以输入一个人的名字、姓氏和出生日期。对于最后一个选项,您必须输入名字、姓氏和出生日期。这可能使用不显眼的验证吗?以下是我的部分

<div id="searchByDemographicsDiv"style="float: left;width: 50%; display:inline; ">
            @*@using(Html.BeginForm("SearchByDemographic", "SearchPatients",FormMethod.Post, new{ id = "searchByDemographics"})){*@
            @using (Ajax.BeginForm("SearchByDemographic", "SearchPatients", null, new AjaxOptions { HttpMethod = "POST", LoadingElementId = Url.Content("~/Images/ajax-loader.gif"), OnSuccess = "binddata", OnFailure = "FailAlert" }, new { id = "searchByDemographics" })){ 
          <ul>
                      <li>@Html.Label("SSN", new { @for = "SSN" })  <br />@Html.TextBox("SSN", null, new { id = "SSN", name = "SSN", @class = "SSN" })</li>
          </ul>
          <ul>
          <p>Or</p>
          </ul>
          <ul>
            <li>@Html.Label("FirstName", new { @for = "FirstName" }) <br />@Html.TextBox("FirstName", null, new { id = "FirstName", @class = "demoGrphcs", name = "FirstName" })</li>
            <li>@Html.Label("LastName", new { @for = "LastName" }) <br />@Html.TextBox("LastName", null, new { id = "LastName", @class = "demoGrphcs", name = "LastName" })</li>
            <li>@Html.Label("dateOfBirth", new { @for = "dateOfBirth" }) <br />@Html.TextBox("dateOfBirth", null, new { id = "dateOfBirth", @class = "demoGrphcs", name = "dateOfBirth" })</li>
            <li>@Html.Label("Address1", new { @for = "Address1" }) <br />@Html.TextBox("Address1", null, new { id = "Address1" })</li>
            <li>@Html.Label("Address2", new { @for = "Address2" }) <br />@Html.TextBox("Address2", null, new { id = "Address2" })</li>
            <li>@Html.Label("City", new { @for = "City" }) <br />@Html.TextBox("City", null, new { id = "City" })</li>
            <li>@Html.Label("State", new { @for = "State" }) <br />@Html.TextBox("State", null, new { id = "State" })</li>
            <li>@Html.Label("Country", new { @for = "Country" }) <br />@Html.TextBox("Country", null, new { id = "Country" })</li>
            <li>@Html.Label("PostCode", new { @for = "PostCode" }) <br />@Html.TextBox("PostCode", null, new { id = "PostCode" })</li>
          </ul>  
        <input type="submit" value="Search By Demographics" class="button" id="submitDemo"/>     
            }
</div>

我本质上希望用户能够通过 SSN 或其他人口统计信息进行搜索,其中 firstName lastName 和 DateOfBirth 是必填字段。如果按 SSN 搜索,我希望它是必填字段。我知道很复杂。

更新**我的表单元素的原始验证**更新

$("#searchByDemographics").validate({
    rules: {
        SSN: {
            require_from_group: [1, ".SSN"]
        },
        FirstName: {
            require_from_group: [1, ".demoGrphcs"]
        },
        LastName: {
            require_from_group: [1, ".demoGrphcs"]
        },
        DateOfBirth: {
            require_from_group: [1, ".demoGrphcs"]
        }
    },
    messages : {
        SSN: 'Please Enter either a valid SSN or Demographic Info below',
        FirstName: 'First name is required',
        LastName: 'Last name is required',
        DateOfBirth: 'Date of Birth is required'
    }
});
4

2 回答 2

0

我不确定在这种情况下为什么要进行不显眼的验证,因为验证非常具体。当您尝试完成的验证是可重用的但不适合您的情况时,不显眼的验证很有意义。

我建议您IValidatableObject在模型中实现服务器端验证并使用纯 jquery 进行客户端验证。

于 2012-09-20T17:19:09.860 回答
0

我认为简单的方法是从模型中传递所有这些值并在 POST 上检查它,根据您的要求进行验证。

于 2012-09-20T14:57:37.473 回答