Error with Decimal Regular Expression validation MVC 5

I have a regular expression to validate a decimal field with two decimals, here de code

[RegularExpression(@"^[0-9]{1,11}(?:\.[0-9]{1,2})?$", ErrorMessage = "El campo {0} debe ser un numero con máximo 2 decimales")]
public decimal? amount { get; set; }

and it's working good when i'm creating a row, but when i'm editing it making a substraction to the field, it throws the ErrorMessage exception, debuging it i can see that the value is a correct one, i mean it has just two decimals and is positive

both fields involved in the substraction are of decimal type

the strangest thing is that it was working well, just stopped working well suddenly

thanks if somebody can give me a hand with this

Upvotes: 0

Views: 272

Answers (1)

Gaétan RYCKEBOER
Gaétan RYCKEBOER

Reputation: 304

/^[0-9]{1,11}(?:\.[0-9]{1,2})?$/ means : “1 to 11 digit, optionnaly followed by an dot and 1 or 2 decimals”.

It cannot work with minus nor plus. “12345678901.12” works, so as “3”, but not “+25” nor “-43.5”. It doesn't handle a decimal value lesser than 1 such as “.42”

Upvotes: 1

Related Questions