JavaScript - Restrict <input> input box to only positive integers or decimals (no characters or punctuation allowed)

foreword

There are many scenarios in the project, you may only want to let the user input positive integers, no decimal points, no letters, punctuation, Chinese, etc.

Of course, being able to enter whole numbers and decimals is also a very common requirement, such as amounts.

solution

The following examples are all checked by regular expressions and are recommended for their simplicity.

1. Only positive integers can be entered, all other characters are invalid:

<input type="text"
onkeyup="value=value

Guess you like

Origin blog.csdn.net/weixin_44198965/article/details/124333691