简体   繁体   中英

JavaScript regular expression currency decimal only

My knowledge of regex is very weak. So far I have manage to create an expression that verifies if a string is an integer or not. The issues is that I only want decimal numbers NO WHOLE numbers at all.

Regex:

"currency": {
                    "regex": /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/,
                    "alertText": "* Not a valid decimal number."
                },

Example

valid:
45.00

invalid:
$45.00
45
$45

You could use:

/^\s*[+-]?(\d+\.\d\d)\s*$/

If you want to allow .50 change \\d+ to \\d* .

If the number of decimal points can be larger than 2 then you can use:

^\s*[+-]?\d*\.\d+\s*$

This will handle 10.50, .50, 10.5123456 etc

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