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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:15:24+00:00 2026-06-15T12:15:24+00:00

I am using VS 2010 with MVC 3 and unobtrusive validation. I am attempting

  • 0

I am using VS 2010 with MVC 3 and unobtrusive validation. I am attempting to create a custom range validator to includes warnings where input values are acceptable but unexpected. (I have working functionality to submit the values with wanings by using the ignore option of the form validator)

The unobtrusive component is:

// The adapter to support ASP.NET MVC unobtrusive validation // 
$.validator.unobtrusive.adapters.add('rangewithwarning', ['min', 'max', 'wmin', 'wmax', 'warning'],
function (options) {
    options.rules['rangewithwarning'] = {
        min: options.params.min,
        max: options.params.max,
        wmin: options.params.wmin,
        wmax: options.params.wmax,
        warning: options.params.warning
    };
    options.messages['rangewithwarning'] = options.message;
});

I have Googled extensively on dynamic error messages which seem to come down to three methods but none of these allow me to display the optional error message. These are:

  1. Returning the error message

    // The validator function
    $.validator.addMethod('rangewithwarning', function (value, element, params) {
    if (!value) {
        return true; // not testing 'is required' here!
    }
    var intValue = parseInt(value);
    // set logic here
    if (intValue >= params.wmin && intValue <= params.wmax) {
        // OK within both actual and range warning
        return true;
    }
    if (params.min <= intValue && intValue <= params.max) {
        //  outside warning but within allowed range - show warning
        return params.warning;
    }
    return $.validator.messages.rangewithwarning;
    

    });

  2. Use showErrors

    // The validator function
    $.validator.addMethod('rangewithwarning', function (value, element, params) {
    if (!value) {
        return true; // not testing 'is required' here!
    }
    var validator = this;
    var intValue = parseInt(value);
    // set logic here
    if (intValue >= params.wmin && intValue <= params.wmax) {
        // OK within both actual and range warning
        return true;
    }
    
    if (params.min <= intValue && intValue <= params.max) {
        //  outside warning but within allowed range - show warning
        var errors = new Object();
        errors[element.name] = params.warning;
        validator.showErrors(errors);            
    }
    
    return false;
    });
    
  3. Return an additional parameter from a messager function

None of these worked although when stepping through the second one the optional message was shown briefly then overwritten.

I’m clearly missing something obvious but cannot see what it is.

Thanks in advance.

  • 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-15T12:15:25+00:00Added an answer on June 15, 2026 at 12:15 pm

    Found a solution which was to use the standard range attribute and then follow it with my new warn only attribute

    (function ($) {
    
    // The validator function
    $.validator.addMethod('rangewarning', function (value, element, params) {
        var localElement = $(element);
        localElement.siblings("span").removeClass("warningOnlyDataOK")
        localElement.removeClass("warningOnlyDataOK")
        if (!value) {
            return true; // not testing 'is required' here!
        }
        var intValue = parseInt(value);
        // set logic here
        if (intValue >= params.wmin && intValue <= params.wmax) {
            // OK within both actual and range warning
            return true;
        }
        //  set display and ignore class items here etc
        localElement.siblings("span").addClass("warningOnlyDataOK")
        localElement.addClass("warningOnlyDataOK")
        return false;
    });
    
    // The adapter to support ASP.NET MVC unobtrusive validation // 
    $.validator.unobtrusive.adapters.add('rangewarning', ['wmin', 'wmax'],
    function (options) {
        options.rules['rangewarning'] = {
            wmin: options.params.wmin,
            wmax: options.params.wmax
        };
        options.messages['rangewarning'] = options.message;
    });
    
    } (jQuery));
    

    Where the class warningOnlyDataOK is used to both display a different style error message and to ignore that validation on save.

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

Sidebar

Related Questions

I am creating a day range validator using DataAnnotations, jQuery.validate and jquery.validate.unobtrusive. I've already
I create a ASP.net MVC 4 project by using Visual Studio 2010 IDE. I
Using VS 2010 beta 2, ASP.NET MVC. I tried to create an Entity framework
I'm using MVC 3 with unobtrusive javascript for client validation. I have a table
I'm using SharePoint 2010. I want to create new view for my list. The
I am currently developing an ASP.NET MVC 3 site using Visual Studio 2010 with
I have been working on an MVC 3 app. I was using VS 2010's
I'm going to be building some ASP.Net MVC 2 software using Visual Studio 2010
Using Visual Studio 2010, MVC project When my form is submitted (currently via javascript,
Setup: I am using MVC 3, EF 4.1, Visual Studio 2010 SP1 with Power

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.