Applying validation to a model in MVC and would like to do it using Regex.
Looking to validate that an ID on my model is greater than 0 on submit.
Applying validation to a model in MVC and would like to do it using Regex.
Looking to validate that an ID on my model is greater than 0 on submit.
I don't know how MVC is relevant, but if your ID is an integer, this BRE should do:
^[1-9][0-9]*$
If you want to match real numbers (floats) rather than integers, you need to handle the case above, along with normal decimal numbers (i.e. 2.5
or 3.3̅
), cases where your pattern is between 0 and 1 (i.e. 0.25
), as well as case where your pattern has a decimal part that is 0. (i.e. 2.0
). And while we're at it, we'll add support for leading zeros on integers (i.e. 005
):
^(0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*)$
Note that this second one is an Extended RE. The same thing can be expressed in Basic RE, but almost everything understands ERE these days. Let's break the expression down into parts that are easier to digest.
^(
The caret matches the null at the beginning of the line, so preceding your regex with a caret anchors it to the beginning of the line. The opening parenthesis is there because of the or-bar, below. More on that later.
0*[1-9][0-9]*(\.[0-9]+)?
This matches any integer or any floating point number above 1. So our 2.0
would be matched, but 0.25
would not. The 0*
at the start handles leading zeros, so 005 == 5
.
|
The pipe character is an "or-bar" in this context. For purposes of evaluation of this expression, It has higher precedence than everything else, and effectively joins two regular expressions together. Parentheses are used to group multiple expressions separated by or-bars.
And the second part:
0+\.[0-9]*[1-9][0-9]*
This matches any number that starts with one or more 0
characters (replace +
with *
to match zero or more zeros, i.e. .25
), followed by a period, followed by a string of digits that includes at least one that is not a 0
. So this matches everything above 0
and below 1
.
)$
And finally, we close the parentheses and anchor the regex to the end of the line with the dollar sign, just as the caret anchors to the beginning of the line.
Of course, if you let your programming language evaluate something numerically rather than try to match it against a regular expression, you'll save headaches and CPU.
What about this: ^[1-9][0-9]*$
Code:
^([0-9]*[1-9][0-9]*(\.[0-9]+)?|[0]+\.[0-9]*[1-9][0-9]*)$
Example: http://regexr.com/3anf5
Reference: https://social.msdn.microsoft.com/Forums/en-US/17089c0f-f9cb-437a-9667-ba8329681624/regular-expression-number-greater-than-0?forum=regexp
If you only want non-negative integers, try:
^\d+$
I think the best solution is to add the + sign between the two brackets of regex expression:
^[1-9]+[0-9]*$
Another solution:
^[1-9]\d*$
\d
equivalent to [0-9]
there you go:
MatchCollection myMatches = Regex.Matches(yourstring, @"[1-9][0-9]*");
on submit:
if(myMatches.Count > 0)
{
//do whatever you want
}
I Tried this one and it worked for me for all decimal/integer numbers greater than zero
Allows white space: ^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
No white space: ^(?=.*[1-9])\d*(?:\.\d{1,2})?$
Reference: Regex greater than zero with 2 decimal places
You can use the below expression:
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\.?\d*$)
Valid entries: 1 1. 1.1 1.0 all positive real numbers
Invalid entries: all negative real numbers and 0 and 0.0
The simple answer is: ^[1-9][0-9]*$
Simplified only for 2 decimal places.
^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
Ref: https://www.regextester.com/94470
I think this would perfectly work :
([1-9][0-9]*(\.[0-9]*[1-9])?|0\.[0-9]*[1-9])
Valid:
1
1.2
1.02
0.1
0.02
Not valid :
0
01
01.2
1.10
[1-9]\.\d{1,2}|0\.((0?[1-9])|([1-9]0?)){1,2}\b
Very simple answer to this use this: \d*