简体   繁体   中英

Regex for number higher than 0 up to 10. integer or with precision of 2

I' have been looking for a regex that will allow the following.

Valid

0.25
0.50
1
1.50
5
8
10
10.00

Invalid

negative numbers
0
0.00
2.555
greater than 10 
10.01

I'm having a hard time getting with regex. Here is that I've been working with:

^-{0}(([0-8].[0-9]+)|[1-10])$ 

...but it just stinks.

Here is the Jquery validation plugin I'm using.

    //regex for 8 hours but not working 100%
jQuery.validator.addMethod("requiredhoursrange", function (value, element) {
    if (/^-{0}(([0-7].[0-9]+)|[1-8])$/.test(value) && $("#Entry_ReasonTypeID").val() == 2) {

        return true;
    }
    return false;
});

$.validator.unobtrusive.adapters.add("requiredhoursrange", function (options) {

    options.rules["requiredhoursrange"] = true; // mvc html helpers
    options.messages["requiredhoursrange"] = options.message;
});

You can go for the following regex:

^(10|10\.00|0\.0[1-9]|0.[1-9][0-9]|[1-9](\.[0-9][0-9])?)$

that will accept 10 or 10.00 or [0.01, 0.09] or [0.10, 0.99] or 1,2,3,4,5,6,7,8,9 eventually followed by .[00, 99] .

I have tested it your patterns and it works.

Valid

0.25
0.50
1
1.50
5
8
10
10.00

Invalid

negative numbers
0
0.00
2.555
greater than 10 
10.01

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM