Reputation: 3463
The regex /^-?\d*(\.\d+)?$/
does the proper validation for both negative and positive integer as well as decimal values. But I want the regex to be extended a bit to address the following also :
The idea is to make sure a single regex does the complete validation instead of doing multiple checks.
Upvotes: 2
Views: 66
Reputation: 4795
Heavily influenced by ilmirons answer:
^(0|-?(0\.\d+|[1-9]\d*(\.\d+)?))$
Upvotes: 1
Reputation: 2942
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Upvotes: 0