2

I have one form inside that form there are two buttons and two text box and after clicking first button validation appears for first textbox field after clicking second button validation of first textbox disappears but with post back .

How can I prevent post back while reseting jquery validation.

      <form id="form1" runat="server">

                    <div id="div1" class="form-horizontal">
                                            <div class="form-group">
                                                <div class="col-md-3">
                                                    <asp:TextBox  ID="TextBox1" runat="server"></asp:TextBox>  
                                                </div>

                                            </div>
                                            <div class="form-group">
                                                <div class="col-md-3">

                                                    <asp:Button ID="Button1"  CssClass="submit btn btn-primary " runat="server" Text="Submit" />
                                                </div>
                                            </div>
                                        </div>


                            <div id="div2" class="form-horizontal">
                                            <div class="form-group">
                                                <div class="col-md-3">

                                                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                                     </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="col-md-3">
                                                    <asp:Button ID="Button2" CssClass="btn btn-primary" runat="server" Text="Submit" />
                                                </div>
                                            </div>
                                        </div>
                </form>




<script>
        $(document).ready(function () {
            $('#Button1').click(function () {
                $('#form1').validate({
                    errorClass: 'help-block animation-slideDown',
                    errorElement: 'div',
                    errorPlacement: function (error, e) {
                        e.parents('.form-group > div').append(error);
                    },
                    highlight: function (e) {
                        $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                        $(e).closest('.help-block').remove();
                    },
                    success: function (e) {
                        e.closest('.form-group').removeClass('has-success has-error');
                        e.closest('.help-block').remove();
                    }, 
                         rules: {
                             'TextBox1': {
                                required: true,
                            },
                        },
                        messages: {
                            'TextBox1': {
                                required: 'ProvideYourCurrentPassword',
                            },
                        },
                });
                $("#TextBox2").rules('remove');
            });
            $('#Button2').click(function () {
                $('#form1').validate({
                    errorClass: 'help-block animation-slideDown',
                    errorElement: 'div',
                    errorPlacement: function (error, e) {
                        e.parents('.form-group > div').append(error);
                    },
                    highlight: function (e) {
                        $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                        $(e).closest('.help-block').remove();
                    },
                    success: function (e) {
                        e.closest('.form-group').removeClass('has-success has-error');
                        e.closest('.help-block').remove();
                    },

                    rules: {
                             'TextBox2': {
                                required: true,
                            },
                        },
                        messages: {
                            'TextBox2': {
                                required: 'ProvideYourCurrentPassword',
                            },
                        },
                });
                $("#TextBox1").rules('remove');
            });
        });
    </script>
1
  • write e.preventDefault() for each button and once formvalidation is done based on valid or not just submit the form or keep it as it is.. Commented Oct 1, 2015 at 12:23

2 Answers 2

1

Try this script, it may help you.

<script>
        $(document).ready(function () {
            $('#form1').validate({
                errorClass: 'help-block animation-slideDown',
                errorElement: 'div',
                errorPlacement: function (error, e) {
                    e.parents('.form-group > div').append(error);

                },
                highlight: function (e) {
                    $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                    $(e).closest('.help-block').remove();
                },
                success: function (e) {
                    e.closest('.form-group').removeClass('has-success has-error');
                    e.closest('.help-block').remove();
                },
            });
            $('#Button1').click(function () {
                $("#TextBox1").rules("add", { required: true, messages: { required: '<%=ValidationMessage.ProvideYourCurrentPassword%>' } });
                $("#TextBox2").each(function () {
                    $(this).rules('remove');
                });
            });


            $('#Button2').click(function () {
                $("#TextBox2").each(function () {
                    $(this).rules('add', {
                        required: true
                    });
                });
                $("#TextBox1").each(function () {
                    $(this).rules('remove');
                });
            });

        });
    </script>
Sign up to request clarification or add additional context in comments.

Comments

0

Post your Jquery Code inside the Sys.Application.add_load

$(function(){
            Sys.Application.add_load(function () { 
           // your Java script Code here
                   });

            });

1 Comment

after putting my jquery code inside Sys.Application.add_load jquery validation stopped working but thanks for reply

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.