regular expression for finding decimal/float numbers?
Optionally match a + or - at the beginning, followed by one or more decimal digits, optional followed by a decimal point and one or more decimal digits util the end of the string:
/^[+-]?\d+(\.\d+)?$/
The right expression should be as followed:
[+-]?([0-9]*[.])?[0-9]+
this apply for:
+1+1.+.1+0.111..10.1
Here is Python example:
import re#print if foundprint(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))#print resultprint(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))
Output:
True1.0
If you are using mac, you can test on command line:
python -c "import re; print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))"python -c "import re; print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))"
You can check for text validation and also only one decimal point validation using isNaN
var val = $('#textbox').val();var floatValues = /[+-]?([0-9]*[.])?[0-9]+/; if (val.match(floatValues) && !isNaN(val)) { // your function}