What is the regular expression for a decimal with a precision of 2?
Valid examples:
123.12
2
56754
92929292929292.12
0.21
3.1
Invalid examples:
12.1232
2.23332
e666.76
The decimal point may be optional, and integers may also be included.
What is the regular expression for a decimal with a precision of 2?
Valid examples:
123.12
2
56754
92929292929292.12
0.21
3.1
Invalid examples:
12.1232
2.23332
e666.76
The decimal point may be optional, and integers may also be included.
Valid regex tokens vary by implementation. A generic form is:
[0-9]+(\.[0-9][0-9]?)?
More compact:
\d+(\.\d{1,2})?
Both assume that both have at least one digit before and one after the decimal place.
To require that the whole string is a number of this form, wrap the expression in start and end tags such as (in Perl's form):
^\d+(\.\d{1,2})?$
To match numbers without a leading digit before the decimal (.12
) and whole numbers having a trailing period (12.
) while excluding input of a single period (.
), try the following:
^(\d+(\.\d{0,2})?|\.?\d{1,2})$
Wrapped the fractional portion in ()?
to make it optional. Be aware that this excludes forms such as 12.
Including that would be more like ^\d+\\.?\d{0,2}$
.
Use ^\d{1,6}(\.\d{1,2})?$
to stop repetition and give a restriction to whole part of the decimal value.
^[0-9]+(\.[0-9]{1,2})?$
And since regular expressions are horrible to read, much less understand, here is the verbose equivalent:
^ # Start of string
[0-9]+ # Require one or more numbers
( # Begin optional group
\. # Point must be escaped or it is treated as "any character"
[0-9]{1,2} # One or two numbers
)? # End group--signify that it's optional with "?"
$ # End of string
You can replace [0-9]
with \d
in most regular expression implementations (including PCRE, the most common). I've left it as [0-9]
as I think it's easier to read.
Also, here is the simple Python script I used to check it:
import re
deci_num_checker = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""")
valid = ["123.12", "2", "56754", "92929292929292.12", "0.21", "3.1"]
invalid = ["12.1232", "2.23332", "e666.76"]
assert len([deci_num_checker.match(x) != None for x in valid]) == len(valid)
assert [deci_num_checker.match(x) == None for x in invalid].count(False) == 0
Main answer is WRONG because it valids 5.
or 5,
inputs
this code handle it (but in my example negative numbers are forbidden):
/^[0-9]+([.,][0-9]{1,2})?$/;
results are bellow:
true => "0" / true => "0.00" / true => "0.0" / true => "0,00" / true => "0,0" / true => "1,2" true => "1.1"/ true => "1" / true => "100" true => "100.00"/ true => "100.0" / true => "1.11" / true => "1,11"/ false => "-5" / false => "-0.00" / true => "101" / false => "0.00.0" / true => "0.000" / true => "000.25" / false => ".25" / true => "100.01" / true => "100.2" / true => "00" / false => "5." / false => "6," / true => "82" / true => "81,3" / true => "7" / true => "7.654"
function DecimalNumberValidation() {
var amounttext = ;
if (!(/^[-+]?\d*\.?\d*$/.test(document.getElementById('txtRemittanceNumber').value))){
alert('Please enter only numbers into amount textbox.')
}
else
{
alert('Right Number');
}
}
function will validate any decimal number weather number has decimal places or not, it will say "Right Number" other wise "Please enter only numbers into amount textbox." alert message will come up.
Thanks... :)