asp.net mvc validation must be a number custom error

前端 未结 10 1147
时光取名叫无心
时光取名叫无心 2021-02-06 01:14

I am new to asp.net and I have a problem. When the users insert in a editor for a decimal field something other than numbers, they get an error \"Field name\" is not a number. B

相关标签:
10条回答
  • 2021-02-06 01:29

    Look for solution at the end of this page:

    http://jwwishart.wordpress.com/2010/03/22/custom-server-and-client-side-required-validator-in-mvc-2-using-jquery-validate/

    I checked this in my MVC 3 RTM project and it works well.

    0 讨论(0)
  • 2021-02-06 01:35

    You can set ResourceClassKey of ClientDataTypeModelValidatorProvider class to name of a global resource that contains FieldMustBeNumeric key to replace mvc validation error message of number with your custom message. Also key of date validation error message is FieldMustBeDate.

    ClientDataTypeModelValidatorProvider.ResourceClassKey="MyResources"; // MyResource is my global resource
    

    See here for more details on how to add the MyResources.resx file to your project:

    The field must be a number. How to change this message to another language?

    0 讨论(0)
  • 2021-02-06 01:37

    This is the actual answer:

    Create a class CustomClientDataTypeModelValidatorProvider. Copy the code from the MVC sources. Change the method MakeErrorString to output the appropiate message like this:

    private static string MakeErrorString(string displayName)
    {
        return string.Format(
            CultureInfo.CurrentCulture, 
            Core.Resources.Errors.EroareNuENr, 
            displayName);
    }
    

    I couldn't find a way not to copy the code just extend it as it uses this static method. If anyone knows this please tell me.

    Then, in global.asax, I wrote this:

    var cdProvider = ModelValidatorProviders.Providers.SingleOrDefault(p => p.GetType().Equals(typeof(ClientDataTypeModelValidatorProvider)));
                if(cdProvider != null)
                {
                    ModelValidatorProviders.Providers.Remove(cdProvider);
                    ModelValidatorProviders.Providers.Add(
                            new CustomClientDataTypeModelValidatorProvider());
                }
    

    so that the flow would actually be routed to my class and not the class in the asp.net MVC dll

    I got the idea from here:

    0 讨论(0)
  • 2021-02-06 01:44

    Unfortunately this is is not a trivial task. However you can try the following hack... Better to do this only on essential fields, as this is more code to maintain.

    In the controller's action method

    if(ModelState.IsValid)
    {
      // code
    }
    else
    {
      if (ModelState["YourField"].Errors.Count > 0)
      {
        ModelState["YourField"].Errors.Clear(); 
        ModelState.AddModelError("YourField", "Your custom message here");
      }
    
      // code
    }
    
    0 讨论(0)
  • 2021-02-06 01:45

    Hope I understand your, to change RangeValidator ErrorMessage just initialize ErrorMessage parameter:

    [Range(0, 100, ErrorMessage = "Some another error message insert here!")]
    [RegularExpression("\d", ErrorMessage = "!!!")]
    public decimal DecimalField { get; set; }
    
    0 讨论(0)
  • 2021-02-06 01:51

    ... or use jQuery to change to message on the client.

    0 讨论(0)
提交回复
热议问题