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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:33:54+00:00 2026-05-31T12:33:54+00:00

Server side validation works great but client side will not work on some fields.

  • 0

Server side validation works great but client side will not work on some fields.

Using :

"jquery-1.5.1.min.js"  
"modernizr-1.7.min.js"  
"jquery.validate.min.js"  
"jquery.validate.unobtrusive.min.js". 

HTML5 semantic markup

MVC3, Razor, Code First

Class Data Annotations:

using System.ComponentModel.DataAnnotations;

[Required, MaxLength(30, ErrorMessage = "First Name must be {1} characters or less")]
public string FirstName { get; set; }

[Range(20, 50, ErrorMessage = "{0} must be between {1} and {2}")]
public double Waist { get; set; }

View:

The view is strongly typed.

@using (Html.BeginForm()) { @Html.ValidationSummary(true)

    <span class="editor-label">  First Name </span>

    <span class="editor-field">

    @Html.EditorFor(model => model.FirstName)

    @Html.ValidationMessageFor(model => model.FirstName)

    </span><br /><br />

    <span class="editor-label">@Html.LabelFor(model => model.Waist): 20 to 50</span>

    <span class="editor-field">

    @Html.EditorFor(model => model.Waist)

    @Html.ValidationMessageFor(model => model.Waist)

    </span><br /><br />

Client side validation works for Waist but not FirstName. Server side works for both.

When I look at the source code Waist has a data-val-range.

data-val-range = “Waist must be between 20 and 50”

There is no data-val-range for FirstName.

Any help greatly appreciated.

Thank you,

Joe

I’m using IE9 64 bit.

Upon further investigation Required and String Length partially work. I changed the Dataannotation attributes to this.

  [Required(ErrorMessage = "First Name is required")]       [StringLength(30, MinimumLength = 2, ErrorMessage = "First Name must be {2} to {1} characters")]     public string FirstName { get; set; }

Now when I go to my edit view and clear the FirstName textbox and tab to the next I get no validation error. If I then enter one character I get the StringLength message, when I subsequently clear the field I then get the Required message. Getting closer but it is still not working correctly.

data-val-length= gets added to the source when I use StringLength

  • 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-31T12:33:56+00:00Added an answer on May 31, 2026 at 12:33 pm

    MaxLength and MinLength doesn’t work with client side validation. You could use StringLength instead:

    [Required]
    [StringLength(30, ErrorMessage = "First Name must be {1} characters or less")]
    public string FirstName { get; set; }
    

    And there’s a MinimumLength property that you could set if you wanted to simulate the MinLength attribute:

    [Required]
    [StringLength(30, MinimumLength = 10, ErrorMessage = "First Name must be between 10 and 30 characters")]
    public string FirstName { get; set; }
    

    UPDATE:

    It seems that you want to trigger the validation eagerly even if the form is not submitted. You could add the following script to achieve that:

    <script type="text/javascript">
        $(function () {
            var settngs = $.data($('form')[0], 'validator').settings;
            settngs.onfocusout = function (element) { $(element).valid(); };
        });
    </script>
    

    UPDATE 2:

    Here’s a full example. Create a new ASP.NET MVC 3 project using the internet template.

    Model:

    public class MyViewModel
    {
        [Required(ErrorMessage = "First Name is required")]
        [StringLength(30, MinimumLength = 2, ErrorMessage = "First Name must be {2} to {1} characters")]
        public string FirstName { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel
            {
                FirstName = "foo"
            });
        }
    
        [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 () {
            var settngs = $.data($('form')[0], 'validator').settings;
            settngs.onfocusout = function (element) { $(element).valid(); };
        });
    </script>
    
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.FirstName)
        @Html.ValidationMessageFor(x => x.FirstName)
        <button type="submit">OK</button>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble with client side validation when using Ajax and jQuery to
I currently have a fairly robust server-side validation system in place, but I'm looking
Which is better to do client side or server side validation? In our situation
I am using Spring MVC on the server side, but in one of the
I have an ASP.NET webform which I want to validate Client-Side and Server-Side, using
Our server side validation (via data annotations) is working great and posts the errors
I'm writing a php forms class with client and server side validation. I'm having
I have added some server side validation to a dropdown box on one of
I am using the solution found here to show client side validation errors in
I'm using the popular jQuery validation plugin: Validation As many know, this plugin works

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.