I would like to validate a decimal
in the below range in my MVC3
application.
0.000000001 to 100.000000000
This is the Regex
I tried but, it is not working:
(^100([.]0{1,9})?)$|(^\\d{1,9}([.]\\d{1,9})?)$
[RegularExpression(@"(^100([.]0{1,9})?)$|(^\d{1,9}([.]\d{1,9})?)$",
ErrorMessage ="Decimal value should be between 0.000000000 to 100.000000000")]
[Range(0, 100, ErrorMessage = " % must be between {1} and {2}")]
[Display(Name = "Percentage:")]
public double? Percentage { get; set; }
Can anyone see what I am doing wrong?
^(1\d\d|[1-9]\d|\d)\.\d{0,8}[1-9]$
In your second alternative you allow 1 to 9 digits before the dot.
(^100([.]0{1,9})?)$|(^\d{1,9}([.]\d{1,9})?)$
^
Change this to
^(100(\.0{1,9})?|\d{1,2}(\.\d{1,9})?)$
You can still "simplify" this, by moving the fraction out of the alternation
^(100|\d{1,2})(\.\d{1,9})?$
be aware that this expression does allow leading zeros like "00.1".
See it here on Regexr
我相信这应该可以解决问题。
\d+\.\d{9}
This will be the required regex:
(\d|[1-9]\d)(\.\d{1,9})?|100(\.0{1,9})?
In your regex, (^\\d{1,9}([.]\\d{1,9})?)$
matches one to nine digits (may be nine 0s upto nine 9s) followed by optional decimal part. This is the problem.
^(?:(?:[1]\d)|(?:[1-9]))?\d\.\d{9}$
This will match your range. Also, it will not match the odd scenario of a double zero preceding the decimal like such: 00.000000000. So, 00.000000000 will not be a valid number.
If you want it with variable decimal places to the right of the 0, then you can use something like:
^(?:(?:[1]\d)|(?:[1-9]))?\d\.\d{1,9}$
edit: sentence wording
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.