'Decimal check on OnKeyUp event

I have couple of text boxes. In those text boxes I need to check few types.

  1. Allow only Numbers and Decimals

Example: 123.2323 //not allowed any characters

  1. Allow only 2 numbers after decimal

Example: 123.23 //not allowed any characters and only allowed 2 digits after decimal

  1. Allow only 1 number after decimal

Example: 123.2 //not allowed any characters and only allowed 1 digits after decimal

I have to check these validation in OnKeyPress event in text boxes.

If possible need all three methods.

Please help me on this.



Solution 1:[1]

If you want you can try this jquery plugin for formatting numbers.

You can set how many decimal places you want. It has nice documentation so it is easy to use.

Solution 2:[2]

Based on the need for this to happen on the KeyUp event, you'll need to use JavaScript. Here's a script for validating numeric only that you can modify for your needs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manie
Solution 2 David