MVC form validation in multiple tabs - auto jump to tab with validation errors?

后端 未结 1 460
生来不讨喜
生来不讨喜 2021-02-06 00:51

I have tabstrip with multiple tabs. In each tab I have a number of text fields for the user to input. The tabstrip is surrounded by a form and just below a submit button.

<
1条回答
  •  长发绾君心
    2021-02-06 01:01

    I don't think that there is out of the box solution for this. But you can do it in javascript pretty easy. What you do is on form submit you look at content of each tab, and if you find validation error then you switch to that tab.

    here is sample:

    
    

    This sample assumes that your form's id is myForm, that your tab id is tabs. Also it assumes that you have ClientValidationEnabled = true and UnobtrusiveJavaScriptEnabled = true in web.config. This code snippet will switch to first tab with error.

    This code is just sample and could be refactored, but it shows idea.

    0 讨论(0)
提交回复
热议问题