开发者

Best way of validating/creating a number only text-box?

开发者 https://www.devze.com 2022-12-17 16:40 出处:网络
I am wanting to have a numbers/currency only textbox control. So basically it should accept this kind of input

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 i开发者_如何学运维t 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


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()


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消