I am getting The value 'abc' is not valid for fieldName. as error message. which is default error message and i want to override it in easier way.
as of now what i have tried is listed below
[RegularExpression(@"^\d+$",ErrorMessage="enter numeric value")][Integer(ErrorMessageResourceType = typeof(appName.Resources.abc.Resource),
ErrorMessageResourceName = "error_numeric")][RegularExpression("([1-9][0-9]*)")]-
Range(1,int.max,ErrorMessage="enter numeric value")
but failed to change default error message.
Suggest me the easiest possible way to do this.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace blueddPES.ViewModels { public class ContactViewModel { [Integer(ErrorMessage="sdfdsf")] public int? hp { get; set; } }
Easiest way is to use Data Annotations Extensions. It has some useful attributes for things like Integers, etc.
Or you could write your own, like in: How to change ‘data-val-number’ message validation in MVC while it generate by helper
Edit: Added complete sample after comments.
I created a sample vanilla MVC 3 project and then did the following:
Added NuGet package
DataAnnotationsExtensions.MVC3Added a Model class:
Added a Home Controller:
Added a Home View:
I hope you get some more insights using this sample code. When I run this sample, it works just as you’d want to expect.