Best way to validate / create textbox only?

I want to have only a text box with numbers / currency.

So basically, it should accept this type of input

  $ 123
 523,100
 $ 12.50
 85.59

and convert it to

  123
 523100
 12.50
 85.59

and should not accept this entry

  F12
 309 $ 2342
 , 102

I looked at the validator validation elements, but I would like this conversion / validation entirely from the client side. What would be the best way to do this?

I would also like for a client error (message box or something else) to appear if they try to enter invalid data and prevent them from leaving bad data in the text box. and then when they get out of focus of the text field, it automatically converts the data to a flat number from the client side.

How would I start to do this? Is there anything in ASP.Net that can help me? Do I just need to use the onfocus event, or which event would be preferable from javascript to test this? I am also inexperienced in Javascript .. so I apologize if this is a bit noobish.

And yes, I will also check server side

+4
source share
2 answers

You can try checking the values ​​inside the text field with isNaN() to check if this number is a number after each key press.

In any case, this problem was answered earlier: Confirm decimal numbers in JavaScript - IsNumeric ()

+1
source

Relying on client-side validation is a security risk if you need to reject certain inputs.

You can use MaskedEditExtender or FilteredTextBox if you use MS Ajax with your site. Otherwise, there are many great javascripts for client validation, especially for use with jQuery.

Just don't ignore server side validation.

0
source

All Articles