Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:05:29+00:00 2026-06-06T08:05:29+00:00

I am building an MVC 3 application which uses the standard Model validation attributes

  • 0

I am building an MVC 3 application which uses the standard Model validation attributes for basic client and server validation. I however also have a form which is in the header and uses the jQuery validate plugin to perform client validation.

When I add the unobtrusive library to the project the header form which uses the validate plugin fails to run and keeps posting. When the unobtrusive library is not included the header validates fine but then the Model validation stops.

Any idea what I am doing wrong?

Edit

Basically i have a login form in the header. I also have a login page that also allows login. The login page is tied to a Model but the form in the header is not, it’s just html. Both forms call the same Controller/Action via jQuery .ajax.

I have lost the ability to use the .ajax method which just doesnt seem to get called since I included the unobtrusive library.

I got the code you included to work, but then I still cant post or perform an action after validation is complete.

My header form is:

<form id="frmHeaderLogin" action="">
<table id="LoginBox" title="Login">
    <tr>
        <td>UserName</td>
        <td><input type="text" id="Username" name="Username" /></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type="password" id="Password" name="Password" /></td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" value="Login" id="btnHeaderLogin" name="btnHeaderLogin" /></td>
    </tr>
</table>
</form>

I have a click event for the submit button which would validate the client input and then submits this to the server after creating a JSON object as the data parameter. The response from the server is also a JSON object. This form is in a layout file as it’s going to be on every page.

The main login page/view has a simple form as below:

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "MainLoginForm" }))
{
<div>
    <fieldset>
       <p style="color:Red;font-size:medium">@ViewData["Message"]</p>
        <legend>Login</legend>
        <div class="editor-label">
        @Html.LabelFor(m => m.UserName, "EmailId")
        </div>
        <div class="editor-field">
        @Html.TextBoxFor(m => m.UserName)
        @Html.ValidationMessageFor(m => m.UserName)
        </div>
        <div class="editor-label">
        @Html.LabelFor(m => m.Password, "Password")  
        </div>
        <div class="editor-field">         
        @Html.PasswordFor(m => m.Password)
        @Html.ValidationMessageFor(m => m.Password)
        </div>
        <p>
        <input type="submit" id="btnMainLogin" value="Login" />
        </p>
     </fieldset>
</div>
}

This also has a jQuery click event which fires the .ajax method and posts a JSON object to the server as above. Both instances return a JSON object.

I suppose at this point the question could be, can I use the same model for the header login which is in a layout file which would allow me to make use of the client and server validation?

The following is an example of the submitHandler that I was using after the validation passed (using jquery.validate)

    $("#formname").validate( {
     // .....
         // .....
         submitHandler: function () {
            var JSONData = new Object();
            $(':text, :password', $("table[id$='LoginBox']")).each(function () {
                JSONData[$(this).attr("id")] = $(this).val();
            });

            $.ajax({
                type: "POST",
                url: "/Controller/Action",
                data: "{'Login': '" + JSON.stringify(JSONData) + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    var response = $.parseJSON(result);
                    alert("Header Login Success!");
                },
                error: function (xhr, status, error) {
                    alert(xhr.statusText + " - ReadyState:" + xhr.readyState + "\n" + "Status: " + xhr.status);
                }
            });
         }
    )};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:05:33+00:00Added an answer on June 6, 2026 at 8:05 am

    If you want to mix Microsoft unobtrusive validation script with custom jquery validate rules that you have written in the same page you will need to add the jquery validate rules to existing form elements. Let’s take an example:

    public class MyViewModel
    {
        [Required]
        public string Foo { get; set; }
    
        public string Bar { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    View:

    @model MyViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#Bar').rules('add', {
                required: true,
                messages: {
                    required: 'Bar is required'
                }
            });
        });
    </script>
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.EditorFor(x => x.Foo)
            @Html.ValidationMessageFor(x => x.Foo)
        </div>
    
        <div>
            @Html.EditorFor(x => x.Bar)
            @Html.ValidationMessageFor(x => x.Bar)
        </div>
    
        <button type="submit">OK</button>
    }
    

    Notice how the Required attribute on the Foo field makes it required and we have defined a custom jquery validate rule for the Bar field.

    And here’s the result when the form is submitted and the 2 fields are left empty:

    enter image description here

    You could of course define any number of custom rules on any field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an MVC application which uses Windows Authentication. I want to handle a
I am building an application in ASP.NET MVC 3 in which I have implemented
I'm building a MVC application which has a heavy client side scripting using Knockout,JQuery
I am building an asp.net mvc application. I have my top menu structure which
I'm building a .NET MVC application which will be deployed on a Windows 2003
I'm building a mvc application which will communicate with flash via AMF, anybody knows
I have an ASP.NET MVC application I'm building and I'm using a Master page.
I am building an MVC application in which I am reading a list of
I'm building a MVC application which is a bit more complex than what I
I'm building ASP.Net MVC application kinda Game which deal a lot with online users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.