The following worked for me. I received the message "Exchange rate must be a number between 0.001 and 9999". It would have been helpful if you posted how you structured your model class.
C#
using System.ComponentModel.DataAnnotations;
// ...
public class Foo
{
[Range(typeof(Decimal), "0.001", "9999", ErrorMessage = "Exchange rate must be a number between {1} and {2}.")]
[RegularExpression(@"\d+?(?:\.\d{3,3})?", ErrorMessage="Exchange rate must be a number.")]
[Required(ErrorMessage = "Exchange rate is required.")]
public decimal ExchangeRate { get; set; }
}
View
@model WebApplication2.Models.Foo
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.ExchangeRate)<br />
@Html.TextBoxFor(model => model.ExchangeRate)<br />
@Html.ValidationMessageFor(model => model.ExchangeRate)<br />
<button type="submit">Submit</button>
}
@section scripts
{
@* Assuming bundle is jqueryval *@
@Scripts.Render("~/bundles/jqueryval")
}
Controller
public ActionResult Test()
{
return View(new Foo());
}
[HttpPost]
[ActionName("Test")]
public ActionResult TestConfirmed(Foo foo)
{
// NOTE: You would need additional logic for dealing with the ModelState.IsValid
// This is for illustration purposes only
return View(foo);
}