1

I'm trying to validate double with regular expressions in data annotations.

I need a regex that validates as it follows:

Pass:

  • 1,00
  • 0,01
  • 0,00
  • 123544,23
  • 266,00

Always 2 decimals.

Fails:

  • ,22
  • 0,
  • ,00
  • 1,0
  • 1,2

So far I've got ^\d+,\d{2}$

But it fails when the numbers ends in zero. For example, 23,00 fails when it should pass.

Note: I use comma as separator because of my culture.

EDIT: I'm using globalization for jQuery Validate. In the view I have this script.

    $(document).ready(function () {
    Globalize.culture('es-AR');

    $.validator.methods.number = function (value, element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    }

    $.validator.methods.range = function (value, element, param) {
        return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
    }

});
Rodrigo
  • 173
  • 1
  • 1
  • 11

1 Answers1

0

Maybe that should work as you wish:

^\d+[,](\d{2})(?<=\d+)$
huembert
  • 49
  • 2
  • 5
  • It works the same as before. doesn't accept 23,00 :/ – Rodrigo Mar 07 '14 at 16:05
  • I tried it again with that expression and it worked for number 23,00, too. '"23,00".match(new RegExp(/^\d+[,](\d{2})(?!=\d+)$/)) -> ["23,00", "00"] - match, ok | "1,00".match(new RegExp(/^\d+[,](\d{2})(?!=\d+)$/)) -> ["1,00", "00"] - match, ok | "123544,23".match(new RegExp(/^\d+[,](\d{2})(?!=\d+)$/)) -> ["123544,23", "23"] - match, ok | ",00".match(new RegExp(/^\d+[,](\d{2})(?!=\d+)$/)) -> null - no match, ok | "1".match(new RegExp(/^\d+[,](\d{2})(?!=\d+)$/)) -> null - no match, ok' – huembert Mar 07 '14 at 17:44
  • In what language are you trying that? – Rodrigo Mar 07 '14 at 19:43
  • In the second place I wrote that code in JavaScript. I tested it by using the Firebug in Firefox. – huembert Mar 07 '14 at 20:03
  • There's something that MVC doesn't like. I tried removing client validation and in server it fails too. If I enter 12,01 works, but if I enter 12,00 does not.. – Rodrigo Mar 07 '14 at 20:15