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 6918327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:50:40+00:00 2026-05-27T09:50:40+00:00

I’m new to MVC3, and for the first time I will validate a form.

  • 0

I’m new to MVC3, and for the first time I will validate a form.
I saw some validation samples using jQuery and using Model.IsValid but I don’t know if this is my case.

I’m using @Html.TextBox, and @Html.ValidationMessage, I can see I need to place 2 jQuery lines in my document to validate:

<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>

And I saw that many people use validation only with jQuery, but I couldn’t get how it really works. So, could you please give me a sample validation code for the Razor View with jQuery (if needed) and for Controller? As I’m not using TextBoxFor, I believe I can’t use validation only with Datannotations in the Model class.

The form I need to validate:

@Html.TextBox("user", "User:")
@Html.TextBox("email", "Email:") <!-- with validation of email string -->
@Html.Password("password")
@Html.Password("passwordConfirm") <!-- with validation if 2 password strings match -->
  • 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-27T09:50:41+00:00Added an answer on May 27, 2026 at 9:50 am

    The jquery.validate.unobtrusive.min.js script works in conjunction with data annotations placed on your model properties. Those validator attributes are then translated by the Html helpers to emit HTML5 data-* attributes which are used by the script. If you don’t have a model decorated with validation attributes you cannot use this script.

    This being said you could still have a model with validation attributes on it and still use the TextBox instead of TextBoxFor. It would be completely stupid and meaningless but you can do it:

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

    and then in your view when you use one of the helpers inside a form validation attributes will be emitted:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.TextBox("MyProperty")
    }
    

    If you don’t have a view model (don’t know why you won’t have a view model as this goes against good practices that I preach for) you could manually wire up validation. In this case you simply remove the jquery.validate.unobtrusive script and use the core jquery validate plugin:

    $(function() {
        $('#id_of_your_form').validate({
            rules: {
                MyProperty: {
                    required: true
                }
            },
            messages: {
                MyProperty: {
                    required: 'Please enter a value for MyProperty'
                }
            }
        });
    });
    

    Obviously the recommended solution is to use a view model and strongly typed helpers:

    public class RegisterViewModel
    {
        public string User { get; set; }
    
        [DataType(DataType.EmailAddress)]
        [Email] // taken from Scott Gu's blog post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
        public string Email { get; set; }
    
        public string Password { get; set; }
    
        [Compare("Password")]
        public string PasswordConfirm { get; set; }
    }
    

    and then in your view:

    @model RegisterViewModel
    <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())
    {
        <div>
            @Html.LabelFor(x => x.User)
            @Html.EditorFor(x => x.User)
            @Html.ValidationMessageFor(x => x.User)
        </div>
        <div>
            @Html.LabelFor(x => x.Email)
            @Html.EditorFor(x => x.Email)
            @Html.ValidationMessageFor(x => x.Email)
        </div>
        <div>
            @Html.LabelFor(x => x.Password)
            @Html.PasswordFor(x => x.Password)
            @Html.ValidationMessageFor(x => x.Password)
        </div>
        <div>
            @Html.LabelFor(x => x.PasswordConfirm)
            @Html.PasswordFor(x => x.PasswordConfirm)
            @Html.ValidationMessageFor(x => x.PasswordConfirm)
        </div>
        <button type="submit">Register</button>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
We're building an app, our first using Rails 3, and we're having to build
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported

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.