There are a lot of regular expressions on the internet about verifying decimals, but many of them are not hundred percent correct, so I combined some of my predecessors ' experience to write one myself.
Validates an infinite number of integers and decimals that are not beginning with 0 (nullable). Integers support unlimited digits, with unlimited digits before the decimal point, and up to two digits after the decimal point.
The JS code is as follows:
var reg =/^ (([^0][0-9]+|0) \. ( [0-9] {.}) $) |^ ([^0][0-9]+|0) $/;
Split separately:
1. Integer:/^ ([^0][0-9]+|0) $/
2. Decimal:/^ (([^0][0-9]+|0) \. ( [0-9] {)}) $/
Can be changed on demand:
1. If you change the limit before the decimal point, change + (for example: limit 4 bits before the decimal point-([^0][0-9]\d{0,3}|0)), integer the same.
2. If you change the limit number after the decimal point, change {--{1} (for example: up to 3 digits after the decimal point, 3}, 3 bits after the decimal point must be reserved--{3}).
Regular expressions that validate integers and decimals