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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:13:24+00:00 2026-05-31T21:13:24+00:00

I try to use the client side validation on a very simple example and

  • 0

I try to use the client side validation on a very simple example and it don’t work. I started from an existing example from the internet.

Here it is (see accepted answer): ASP .Net MVC 3 unobtrusive custom client validation

Basically, it uses this blog as solution: http://benjii.me/2010/11/credit-card-validator-attribute-for-asp-net-mvc-3/

So I started from a blank solution and would like to validate that a price is greater than a minimum.

My controller:

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

    [HttpPost]
    public ActionResult Index(MinPriceViewModel model)
    {
        if (!ModelState.IsValid)
            return View(model);

        return Content("Thank you very much");
    }
}

My view model:

public class MinPriceViewModel
{
    [MinPrice(MinPrice=9.99)]
    [Required]
    public double Price { get; set; }
}

The attribute for validation (client & server):

public class MinPriceAttribute : ValidationAttribute, IClientValidatable
{
    public double MinPrice { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
            return true;

        var price = Convert.ToDouble(value);

        if (price < MinPrice)
        {
            return false;
        }

        return true;
    }

    public override string FormatErrorMessage(string name)
    {
        return "Attention le champ " + name + " ne contient pas un prix acceptable.";
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage,
            ValidationType = "minprice"
        };
    }
}

The view:

@model MinPriceValidation.Models.MinPriceViewModel

@{
    ViewBag.Title = "Home Page";
}

<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        jQuery.validator.unobtrusive.adapters.addBool('minprice');
    }); 
</script>

@using(Html.BeginForm())
{
    @Html.LabelFor(m => m.Price)
    @Html.TextBoxFor(m => m.Price)
    @Html.ValidationMessageFor(m => m.Price)

    <input type = submit />
}

I placed a breakpoint on my post action in my controller to check whether the client or server validation occurred. When I run the solution and I type 1 and submit, the breakpoint is reached in my controller (post action) >> I concluded that client validation is never used.

I didn’t understand because I started from the basic example (credit card validation) and I know this credit card validation works in client validation (I tested it in another solution).

I’m a bit lost.

Any idea why the client validation did not work?

Thanks.


UPDATE

After googling a lot, I see that some people tells we need a javascript function for the client validation like this:

    jQuery.validator.addMethod("minprice", function (value, element, param) {
        // Perform tests here for client validation
        return (value>10);
    });

    jQuery.validator.unobtrusive.adapters.addBool('minprice');

When I test with this added javascript function it works but it seems strange to me that the creait card example doesn’t have any javascript function in the view for checking the credit card!!

I’m still lost.

  • 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-31T21:13:26+00:00Added an answer on May 31, 2026 at 9:13 pm

    When you say that you are seeing people using this with credit card and it works without the need of a custom javascript function I suppose you are reffering to the following post.

    A credit card validation rule is directly built in the jquery validate plugin. That’s why you don’t need to write a custom validation function.

    As far as a minprice rule is concerned, there’s no such thing, so you need to write a custom javascript function to define what minprice means.

    This being said I get the feeling that you are reinventing some wheel with this attribute as you could simply do:

    [Range(9.99, double.MaxValue)]
    [Required]
    public double Price { get; set; }
    

    and since we are talking about prices, it is recommended to use a decimal type on your model:

    [Range(9.99, double.MaxValue)]
    [Required]
    public decimal Price { get; set; }
    

    Now you will get automatic client side and server side validation.

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

Sidebar

Related Questions

I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I have done extensive client-side validation through the help from jQuery. Now come to
Here is client Side code: jQuery(document).ready(function(){ jQuery(#list).jqGrid({ url:'example.php', datatype: 'xml', mtype: 'GET', colNames:['Inv No','Date',
I'm using the [System.Web.Script.Services.ScriptService] tag to use web services callable from client side javascript.
I try to use This Way to fire a custom validator in client side
I am trying to use the Client-side Validation with jQuery in ASP.NET MVC (2).
I want as well as Client Side Validation as Server Side Validation. I realized
I have a relatively simple program where I try establish Client Server connection and
I have the following code on the client side for retrieving data from the
I'm trying to create a cookie on client side using wininet from a c#

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.