0

In my wizard I am using three buttons Next Previous and Finish like

<div class="wizard-footer">
            <div class="pull-right">
                <input type='button' class='btn btn-next btn-fill btn-success btn-wd btn-sm' name='next' value='Next' />
                <input type='submit' id='btnSubmit' class='btn btn-finish btn-fill btn-success btn-wd btn-sm' name='finish' value='Finish' />

            </div>
            <div class="pull-left">
                <input type='button' class='btn btn-previous btn-fill btn-default btn-wd btn-sm' name='previous' value='Previous' />
            </div>
            <div class="clearfix"></div>
        </div>

Next and Previous input type is button and Finish is submit type. So when I click on Submit all data annotation validation validate the form according to the validation rule I defined in Modes. Now I want to validate each step means when I click Next button all the Model validation should validate the form but when I click Previous validation should ignore.

Please help me on how to validate the form when Input type is Button in MVC. I google a lot but did not find any proper solution.

6
  • possible duplicate of How can I programmatically trigger client side validation set up by a Razor view? Commented Feb 24, 2015 at 11:31
  • No it's not working for me. I already tried it. Commented Feb 24, 2015 at 11:34
  • Well that's the way to do it, and it doesn't show from your question that you tried that. If it didn't work, show all relevant code and explain what it did or didn't do. Commented Feb 24, 2015 at 11:35
  • on click of next button use $("#FormID").valid(), is this are you looking for? Commented Feb 24, 2015 at 11:36
  • If I understood your question properly, then you're looking for some script that can trigger your validations on client side when you click on next button, right? Commented Feb 24, 2015 at 11:39

1 Answer 1

3

try bellow code,

$(".btn-next").click(function() {
    if ($("form").valid()) {
        //Some code to move on next tab
    } else {
        //Do something if form is InValid
        // Error messages will be auto populated, in case if you want some more furniture, use this block :)
    }
});

Demo

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

8 Comments

Let me check it. I need something like this only.
Actually this is validating the form but it's not showing any error message. I have done data annotation validation with this form. Is there any way those error message will display when validation failed ?
Yes this way it will populate error message too, just check that fiddle
I'm assuming client side validation is enabled in your page.
I have checked it but error is not displaying. It is only preventing the page to submit.
|

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.