Please help with me writing a JavaScript Validation for currency/money field.
So please provide any regular expressions if u have 🙂
Also, for my region, don’t need any currency symbols like ‘$’ in the field.
Only decimals are to be included for validation as special chars., along with numbers.
You could use a regexp:
If you’re not looking to be as strict with the decimal places you might find it easier to use the unary (
+) operator to cast to a number to check it’s validity:If the number string is invalid, it will return
NaN(Not a Number), something you can test for easily:It will, of course, allow a user to add more decimal places but you can chop any extra off using
number.toFixed(2)to round to 2 decimal places.parseFloatis much less strict with input and will pluck the first number it can find out of a string, as long as that string starts with a number, eg.parseFloat("123abc")would yield123.