ASP.Net MVC 3 client side validation with a 3 tabs

2019-07-27 10:46发布

I have to build a registration form using asp.net mvc 3 jquery validate. That form is composed of approx 20 fields splitted in 3 JS tabs due to UI and navigation style. I wrote all my validation on the serverside through annotations in the profile model:

namespace Web.Models
{
public class ProfileModel
{

    //companyName
    [Required(ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyNameRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyNameLong")]
    [Display(Name = "CompanyName_label")]
    public string companyName { get; set; }

    //companyAddress
    [Required(ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyAddressRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyAddressLong")]
    [Display(Name = "CompanyAddress_label")]
    public string companyAddress { get; set; }

    //companyCity
    [Required(ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyCityRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyCityLong")]
    [Display(Name = "CompanyCity_label")]
    public string companyCity { get; set; }

    //companyZip
    [Required(ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyZipRequired")]
    [StringLength(10, ErrorMessageResourceType = typeof(Core.Resources.Resources),
        ErrorMessageResourceName = "CompanyZipLong")]
    [Display(Name = "CompanyZip_label")]
    public string companyZip { get; set; }

... and so on for a toltal 20 fields ...

My validation labels are in our Core->Resources localized files, the profile controller is coded to catch HTTPPOST and my view looks like this:

@model Web.Models.ProfileModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.ValidationSummary(true)

@using (Html.BeginForm("Register", "Profile", FormMethod.Post, new { id = "RegForm" }))
{

    <div id="tabs">
    <ul>
        <li><a href="#tabs-1">Company</a></li>
        <li><a href="#tabs-2">User</a></li>
        <li><a href="#tabs-3">Address</a></li>
    </ul>

        <fieldset>

        <div id="tabs-1"> 

        <div class="editor-label">
            @Html.LabelFor(model => model.companyName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.companyName)
            @Html.ValidationMessageFor(model => model.companyName)
        </div>
    ... here other fields ...

    </div>
    <div id="tabs-2">

        <div class="editor-label">
            @Html.LabelFor(model => model.userFName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.userFName)
            @Html.ValidationMessageFor(model => model.userFName)
        </div>
    ... here other fields ...

As you can see I use a single ProfileModel with all fields defined, a single "big" form splitted over 3 tabs (Company, User, Address). Now I have to put a NEXT button at the bottom of first tab, a PREV and NEXT buttons at the bottom of second tab, a finally a PREV and SUBMIT button at the bottom of third and last tab.

My question is how to implement Client side scripting to prevent the user skips to tab2 even if tab1 is not validated (partial validation). So I need to browse throught tabs only if the previous are correctly validated. Any idea?

2条回答
放荡不羁爱自由
2楼-- · 2019-07-27 10:53

I know this is an old question, but for anyone who may be looking for similar. But would you be open to rethinking the design? From your description, what you really need is a wizard rather than tabs. Have a look at JQuery wizards. You want to use the validation Option of the Wizard plug-in.

I had a link to JQuery Wizard on some site, but removed it as I got voted down as it appears that 5 years later the link was compromised.

查看更多
Summer. ? 凉城
3楼-- · 2019-07-27 11:00

You would start with the concepts in Eagerly Performing ASP.NET MVC 3 Unobtrusive Client Side Validation but modify it to use work with your next button clicks.

Specifically it centers around using .valid() against the form which contains elements with validation attributes which I've copied below in case the article ever disappears.

<script type="text/javascript">
          $(document).ready(function () {
                    $('input','form').blur(function () {
                              $(this).valid();
                    });
          });
</script>
查看更多
登录 后发表回答