I have a form that only takes in numbers as it represents a dollar amount. Currently, the form only accepts numerical keystrokes. Is this good practise?

  • 1
    Currently the user can get around it by using pasting other values in the form. Also, this is breaking keyboard shortcuts like Ctrl-V, but I am working to fix these issues. I am also aware that data should be verified on the server
    – Casebash
    Sep 19 '11 at 7:28
  • Actually if validation is that simple it should be validated with Javascript on the client (then at the server in case someone sends raw data/with JS disabled.) JS also lets you give immediate feedback if they enter an invalid character. It's definitely NOT good usability to let them accidentally enter '!00' in a number only field, then make them re-enter it
    – Ben Brocka
    Sep 19 '11 at 11:55
  • 4
    also if breaking copy/paste is an issue (say they copy "$30" and you need "30") you can have Javascript on the form dynamically trim out the non-numerical values, and maybe show a little helper text stating that non-numeric values were removed.
    – Ben Brocka
    Sep 19 '11 at 12:34
  • @BenBrocka: I meant verified on the server in addition to the client
    – Casebash
    Sep 19 '11 at 12:36

Yes, I would say that you should only accept characters appropriate to the field. If it's a phone number, you should also accept characters like: ()-+.

However, you should let the person know that an invalid key was pressed rather than do nothing. Otherwise you may end up frustrating them. Examples include: a small error message that pops up next to the field and then fades out; or simply changing the border colour to red before fading it back again.

  • 2
    I would advise caution on assuming you know all the valid characters for a particular field. In a phone number field, is it bad data to write "0123 456 (ask for extension 19)"? Or "Call patient's daughter: 0987 654"? May 10 '13 at 9:06
  • 1
    @vincebowdren Good point for phone numbers, but that wasn't the op's question. He was referring to dollar input (which would typically need to be ready and manipulated programatically).
    – JohnGB
    May 10 '13 at 12:21
  • 2
    Agreed. But it's worth considering the purpose to which any data item will be put, before deciding on validation rules. A dollar value might be net or gross, might be per item or by weight, might be debit or credit (any of which might affect the way the user thinks they should be entering the data). What sounds like a simple issue might have complex requirements and subtle use cases - which the OP should think through whatever kind of field they are thinking of validating. May 10 '13 at 12:31

Client-side validation can be helpful, but you need to be very careful how you implement it; it's easy to make it unhelpful to the user.

For example, if you silently ignore non-numeric characters, then a user quickly typing '12.34' might not realise that the field has ignored their decimal point and accepted the text as '1234'.

Another example; if you silently ignore non-numeric characters, then a user typing in a negative number '-56' will find it converted into a positive number '56'.

Now, it may well be invalid for the system to accept fractional numbers or negative numbers; but you need to think how to best help the user when they enter these. Are they deliberately trying to enter a value they believe to be valid? Are they accidentally typing a value which they know to be invalid? Does your validation routine help the user realise and correct their mistake?

  • The most sensible answer to date. Too much validation kills validation. May 10 '13 at 9:01

Short answer: Yes, I believe this is indeed good practice. Mind that the decimal sign (possibly depending on localization settings) or a negative sign might also be regarded as correct input.

Also check out this article: http://msdn.microsoft.com/en-us/library/windows/apps/aa511451.aspx#passwordsAndPINs especially the part about "Incorrect character".


Like @Bart, I thought to the decimal separator : “,” or “.”

It may be a good idea to accept the signs “+” and “−”, and “-” of course [“−” is a “minus” sign, and “-” is a hyphen].

The same goes for the space, and some of its variants : non-breaking space, thin space… When I want to transfer 1 million dollars, I type “1 000 000”.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.