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

  • SEARCH
  • Home
  • 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 5952789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:43:53+00:00 2026-05-22T17:43:53+00:00

I have an extremely simple example trying to use Data Annotations with unobtrusive javascript

  • 0

I have an extremely simple example trying to use Data Annotations with unobtrusive javascript in MVC3:

Model:

public class RegisterModel {
    [Required]
    public string EmailAddress { get; set; }

    [Required]
    public string FirstName { get; set; }
}

Controller:

public class HomeController : Controller {
    public ActionResult Index() {
        return View(new RegisterModel());
    }

    [HttpPost]
    public ActionResult Index(RegisterModel model) {
        return View(model);
    }
}

View:

@model RegisterModel

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using( Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "mainForm" } ) ) {
    @Html.EditorFor( m => m.FirstName)<br/>
    @Html.ValidationMessageFor( m => m.FirstName)<br/>

    @Html.EditorFor(m => m.EmailAddress)<br/>
    @Html.ValidationMessageFor(m => m.EmailAddress)<br/>

    <input type="submit" value="Submit" />
}

Everything works fine. When I hit the submit button while the form is empty it prevents me from submitting. It outputs unobtrusive javascript and does client validation. I wanted to do some remote validation to make sure the email address is not in use so I added the following to the view:

<script type="text/javascript">
    $("#mainForm").validate({
        rules: {
            EmailAddress: {
                email: true,
                remote: {
                    url: "@Url.Action("CheckForValidEmail")",
                    type: "post"
                }
            }
        },
        messages: {                
            EmailAddress: { 
                email: "You must provide a valid e-mail address.",
                remote: jQuery.format("The email {0} is already in use.")
            }
        }
    });
</script>

As soon as I add this my validation stops working. I think it is because I’m mixing the data annotations validation with the jquery validation.

If I remove the [Required] attributes from the model and add this to my script then everything works:

FirstName: { required: true },
EmailAddress: { required: true, email: true, remote ... }

Everything works fine.

Do I have to give up unobtrusive javascript validation to use this remote validation? Is there a way to do remote validation with unobtrusive javascript?

  • 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-05-22T17:43:54+00:00Added an answer on May 22, 2026 at 5:43 pm

    Yes the problem comes from the fact that you are attaching another validate handler to the form which conflicts with the one attached by the jquery unobtrusive validation.

    To achieve this you could use the Remote attribute:

    public class RegisterModel {
        [Required]
        [Remote("CheckForValidEmail", "Home")]
        public string EmailAddress { get; set; }
    
        [Required]
        public string FirstName { get; set; }
    }
    

    and the CheckForValidEmail action on HomeController:

    public ActionResult CheckForValidEmail(string emailAddress)
    {
        if (repository.IsValid(emailAddress))
        {
            // If you return a JSON string the model will be considered
            // invalid and this string will be used as error message
            return Json(
                string.Format(
                    "The email {0} is already in use.", 
                    emailAddress
                ), 
                JsonRequestBehavior.AllowGet
            );
        }
        // Return true to indicate that the email address is valid
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    

    Now you no longer need to write any javascript.

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

Sidebar

Related Questions

I have an extremely simple unit test. I'm not sure why it fails when
I have this extremely simple splash page here: http://iph0wnz.com It has the main graphic
I have an Objective-C view controller class, from which I am trying to call
I have some extremely weird behavior that seems to result in silent exceptions. How
I am trying something very simple, but for some reason it does not work.
I am about at wits end with what should be an extremely simple issue.
Am I missing something? I'm trying to do a simple ajax call using jquery
I have an existing site that is working well with Authlogic login. I'm trying
I have found this example on StackOverflow: var people = new List<Person> { new
I have a class that is used as part of a financial application. The

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.