Skip to main content
added 1030 characters in body
Source Link
Vadim
  • 18k
  • 5
  • 40
  • 62

AddHere's a class="required" attributesolution. It mimics what mvc does to thehook into jQuery validation. So there's a checkbox called Accept that doesn't belong to the model. The script must go after the form and it adds all the validation meta data for that field.

    <%
        Html.EnableClientValidation(); %>
    <% using(Html.BeginForm("Show"))
{ %>
      <%= Html.EditorForModel() %>
      <div class="editor-field">
        <%= Html.CheckBox("Accept", new { @class = "required" })%> 
        <span class="field-validation-valid" id="Accept_validationMessage"></span>
      </div>
      <input type="submit" value="Submit" />
<%} %>
<script type="text/javascript">
    window.mvcClientValidationMetadata[0].Fields.push({
        FieldName: "Accept",
        ReplaceValidationMessageContents: true,
        ValidationMessageId: "Accept_validationMessage",
        ValidationRules: [{ ErrorMessage: "The Accept field is required.", ValidationType: "required", ValidationParameters: {}}]
    });
</script>

Add a class="required" attribute to the checkbox.

Here's a solution. It mimics what mvc does to hook into jQuery validation. So there's a checkbox called Accept that doesn't belong to the model. The script must go after the form and it adds all the validation meta data for that field.

    <%
        Html.EnableClientValidation(); %>
    <% using(Html.BeginForm("Show"))
{ %>
      <%= Html.EditorForModel() %>
      <div class="editor-field">
        <%= Html.CheckBox("Accept", new { @class = "required" })%> 
        <span class="field-validation-valid" id="Accept_validationMessage"></span>
      </div>
      <input type="submit" value="Submit" />
<%} %>
<script type="text/javascript">
    window.mvcClientValidationMetadata[0].Fields.push({
        FieldName: "Accept",
        ReplaceValidationMessageContents: true,
        ValidationMessageId: "Accept_validationMessage",
        ValidationRules: [{ ErrorMessage: "The Accept field is required.", ValidationType: "required", ValidationParameters: {}}]
    });
</script>
Source Link
Vadim
  • 18k
  • 5
  • 40
  • 62

Add a class="required" attribute to the checkbox.