My requirement is to test the pasted data and if it fails then don't paste.
Regex: /\d{0,4}([\.|\,]\d{0,2})?/
Data used:
1.2 tests true
1.2.3 test true as well
Requirement is
min 0 max 4 digits before decimal point
decimal point can be either dot or comma
min 1 max 3 digits after decimal point if there exists a decimal point.
I have tried following but does not work.
Any help will be appreciated.
fiddle
My requirement is to test the pasted data and if it fails then don't paste.
Regex: /\d{0,4}([\.|\,]\d{0,2})?/
Data used:
1.2 tests true
1.2.3 test true as well
Requirement is
min 0 max 4 digits before decimal point
decimal point can be either dot or comma
min 1 max 3 digits after decimal point if there exists a decimal point.
I have tried following but does not work.
Any help will be appreciated.
fiddle
Share Improve this question asked Sep 8, 2016 at 9:46 niklodeonniklodeon 1,3807 gold badges25 silver badges52 bronze badges1 Answer
Reset to default 22From your requirements
/^\d{0,4}(?:[.,]\d{1,3})?$/
^
: Start of the line\d{0,4}
: Zero-to-four digits[.,]
: Match dot or comma\d{1,3}
: One-to-three digits(?: ... )
: Non-capturing group(something)?
The group can occur zero or once$
: End of line
input:valid {
color: green;
font-weight: bold
}
input:invalid {
color: red;
}
<input type="text" pattern="\d{0,4}(?:[.,]\d{1,3})?" />