0

I want to disable validation some textboxes when certain button is clicked for instance , If i have got 4 textboxes , when I click button 1 only first 2 textbox should validate and when I click button 2 only the last 2 textboxes should validate , Currently all the boxes are validating , How can i enable / disable textbox validation using jquery , The validation only activate on the textboxes which are visible in the UI not the hidden one I googled it and found some thing like this :

 <script type="text/javascript">
document.getElementById("YourbuttonID").disableValidation = true;
</script>

Below is the code which I am using :

<script type="text/javascript">

$(document).ready(function () {
    var $startdates = $('#startDates');
    var $endDates = $('#endDates');
    var $showEvents = $('#showEvents');
    $startdates.hide();
    $endDates.hide();
    $showEvents.hide();

    $('#hide').click(function () {
        $startdates.show();
        $endDates.show();
        $('#showEvents').show();
        $('#eventdids').hide();

        $(this).hide();
        return false;

    });

    $("#hide").validate({
        ignore: "#hide"
    })

    $('#showEvents').click(function () {
        $startdates.hide();
        $endDates.hide();

        $('#eventdids').show();
        $('#hide').show();
        $(this).hide();
        return false;

    });
});
 </script>
<tr id="startDates">
        <td>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.StartDate) %>
        </div>
        </td>
        <td>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.StartDate) %>
            <%: Html.ValidationMessageFor(model => model.StartDate) %>
        </div>
        </td>
        </tr>

        <tr id="endDates">
        <td>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.EndDate) %>
        </div>
        </td>
        <td>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.EndDate) %>
            <%: Html.ValidationMessageFor(model => model.EndDate) %>
        </div>
        </td>
        </tr>
<tr id="eventdids">
        <td>
        <label>Events</label>
        </td>
        <td>
         <% foreach (var item in (SelectList)ViewData["events"]) { %>
                 <input type="checkbox" name="Name" value="<%=item.Value %>" />
                  <label for="<%=item.Value%>"><%=item.Text%></label>
                  <br />

        <% } %> 

        </td>
        <td><input type="button" name="Select" id="hide" style="width:auto" value="Select All Events" /></td>


        </tr>

        </table>
      <input type="button" name="show" id="showEvents" style="width:auto" value="Show All Events" />

        <p>
            <input type="submit" value="Create" />
        </p>

1 Answer 1

1

i wouldn't play with hiding elements + it will fail at the server validation - these fields are still required according to your model

you will have to create a dependent validation for handling both cases this might help youconditional-validation-in-asp-net-mvc-3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.