简体   繁体   中英

Regular expression or complete javaScript to validate Salary (a 'Double' number)?

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 user 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, eg 150,000 , then try replacing \\d{1,6} with

\d{1,3}(?:,?\d{3})?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM