How to validate such number input with one RegEx. Strings are not allowed. Two decimal positions after dot or comma.
Example:
123.34
1.22
3,40
134,12
123
Try this regex:
/^(\d+(?:[\.\,]\d{2})?)$/
If $1
exactly matches your input string then assume that it is validated.
Try This,
/^(\d+(?:[\.\,]\d{1,2})?)$/
pat = re.compile('^\d+([\.,]\d\d)?$')
re.match(pat, '1212')
<_sre.SRE_Match object at 0x91014a0>
re.match(pat, '1212,1231')
None
re.match(pat, '1212,12')
<_sre.SRE_Match object at 0x91015a0>
This is my method to test decimals with ,
or .
With two decimal positions after dot or comma.
(\d+)
: one or more digits(,\d{1,2}|\.\d{1,2})?
: use of .
or ,
followed by 2 decimals maximumconst regex = /^(\d+)(,\d{1,2}|\.\d{1,2})?$/;
console.log("0.55 returns " + regex.test('0.55')); //true
console.log("5 returns " + regex.test('5')); //true
console.log("10.5 returns " + regex.test('10.5')); //true
console.log("5425210,50 returns " + regex.test('5425210,50')); //true
console.log("");
console.log("10.555 returns " + regex.test('10.555')); //false
console.log("10, returns " + regex.test('10,')); //false
console.log("10. returns " + regex.test('10.')); //false
console.log("10,5.5 returns " + regex.test('10,5.5')); //false
console.log("10.5.5 returns " + regex.test('10.5.5')); //false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With