I want to validate salary text field with java script in my html page having syntax like
Max six digits, a dot, max two digits after dot
and it can be done in two ways:
1) I make u开发者_如何学Pythonser only enter integer values and append a dot and two zeros at the end
by some javaScript code
.
2) Check the entry given by user at time of formValidation
with a regex
that can pass the right syntax as i described above...
Which will be the simpler one? And how can it be achieved?
Max six digits, a dot, max two digits after dot
/^\d{1,6}(?:\.\d{0,2})?$/
It actually means between 1 and six digits, followed by optionally a dot and a max of two digits.
If you want to require the dot, then use
/^\d{1,6}\.\d{0,2}$/
If you want to allow commas, e.g. 150,000
, then try replacing \d{1,6}
with
\d{1,3}(?:,?\d{3})?
精彩评论