4

I am wanting to have a numbers/currency only textbox control.

So basically it should accept this kind of input

$123
523,100
$12.50
85.59

and convert this to

123
523100
12.50
85.59

and should not accept this input

F12
309$2342
,102

I have looked at validator controls but I would like to this conversion/checking completely from the client side. What would be the best way of doing this?

I also would like a clientside error(message box or something) popped up if they try to enter invalid data and prevent them from leaving the bad data in the textbox. and then when they get out of focus of the textbox it automatically converts the data into a flat number from the client side.

How would I get started in doing this? Is there something in ASP.Net that can help me? Would I need to just use the onfocus event or what event would be preferable from javascript to validate this? I'm rather inexperienced in Javascript also.. so I apologize if this is a bit noobish.

And yes, I will also validate on the server-side

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • 5
    I wouldn't solely rely on client-side validation, it's a good idea to give instant user-feedback but not for beeing sure the data is normalized before processing. – neo Jan 19 '10 at 17:11
  • Well yes of course there would basically just be a `float.parse` type thing which if it fails then the application dies. It would be silly to not check the user input on the server side – Earlz Jan 19 '10 at 17:13

2 Answers2

1

You could try validating values inside a textbox using isNaN() to check if the value is a number, after each keystroke.

Anyways, this issue has been answered before here: Validate decimal numbers in JavaScript - IsNumeric()

Community
  • 1
  • 1
0

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

You could use the MaskedEditExtender or FilteredTextBox if you're using MS Ajax with your site. Otherwise there are lots of great javascripts out there to do client validation, especially for use with jQuery.

Just don't ignore validation on the server side.

womp
  • 115,835
  • 26
  • 236
  • 269