0

I am not sure if this can be achieved via Regex Edit Alone. I need to validate a integer field value for multiples of 10 in my MVC View. Is this achievable via regex alone and unobstrusive validation?

Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79

3 Answers3

8

A pattern like this should work:

^[0-9]*0$

Of course this will allow 0. If you'd like to ensure that the input is greater than or equal to 10, you can use this:

^[1-9][0-9]*0$

However, this won't handle numbers expressed in different numeral systems (e.g. ۱۰, which I believe is 10 in Eastern Arabic numerals). It would be better to create a custom validation attribute to test the value of the input. A simple implementation would look a bit like this:

public class MultipleOf10Attribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return ((int)value) % 10 == 0;
    }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
3

Hmm. It's my understanding that multiples of 10 end in 0. Always. (At least in decimal.) So how about

^.*0$

or if you want to make sure they're all decimal:

^\d*0$

simple, unless I'm missing something.

As per the comments, matching negative numbers:

^-?\d*0$

For a more generalized (and totally useless) approach, check out: Check number divisibility with regular expressions

And a cool way to check for primes, again useless here

Community
  • 1
  • 1
vroomfondel
  • 3,056
  • 1
  • 21
  • 32
0

yes you can do that here is the regex for multiple of 10

^\d*\d?((10)|(0))\.?((0)|(00))?$
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • 1
    That seems like a complicated way of doing it.. also OP says "integer field", and you appear to be allowing decimals – Blorgbeard Aug 28 '13 at 00:29