views:

37

answers:

3

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

  1. Allow only Numbers and Decimals

    Ex: 123.2323 //not allowed any characters

  2. Allow only 2 numbers after decimal

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

  3. Allow only 1 number after decimal

    Ex: 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 3 methods.

Please help me on this. Thanks in advance.

+3  A: 

Regular expressions and Quirksmode.org are your friends here.

Also, something like jQuery might help with validation.

cofiem
JQuery - always good for simplifying things...
David Stratton
A: 

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.

David Stratton
A: 

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.

Manie