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?
-
4What's wrong with something like `[0-9]*0`? – David Schwartz Aug 27 '13 at 22:12
-
@DavidSchwartz sometimes answer is right in front of youand is simple :) – Flood Gravemind Aug 27 '13 at 22:14
-
1Why don't use the module? 5%10 != 0 and 50%10 == 0 son any multiple of ten will allways have module 0 – Gonzix Aug 27 '13 at 22:16
3 Answers
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;
}
}

- 146,324
- 30
- 291
- 331
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

- 1
- 1

- 3,056
- 1
- 21
- 32
-
3
-
-
@Blorgbeard, thanks for the tip -- I assumed "integer field" meant that all input would be integral. – vroomfondel Aug 27 '13 at 22:16
-
@rogaos you may be right actually, doesn't hurt to make the regex clearer though :) – Blorgbeard Aug 28 '13 at 00:28
yes you can do that here is the regex for multiple of 10
^\d*\d?((10)|(0))\.?((0)|(00))?$

- 15,504
- 1
- 29
- 47
-
1That 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