Validating a text field containing a float is a valid percentage value?

后端 未结 3 1322
北海茫月
北海茫月 2021-01-29 05:00

I have a form text field that has a KeyUp event. On KeyUp I\'m ignoring anthing but numbers, the period, backspace, delete and cursor keys. So, the only thing in the field ca

3条回答
  •  一整个雨季
    2021-01-29 05:48

    You could limit the length of your field to 5, check that the value can be cast to a number, and then verify that the number is between 0 and 100. You won't be able to enter 100.00, but why would you need to?

    Also, here's a fiddle for testing. One note, if you really care about validating "95." as false, you'll have to add extra validation for that. However, I'd advise against this requirement, because it is still represents a valid percentage and won't affect the outcome of calculations.

提交回复
热议问题