Input number validation - Restrict to enter only numbers or digits, int & float both

How to restrict the input field to enter only numbers/digits int and float both.
Sometimes we need to allow both integer as well as float value for fields like amount, so in that case the validation is required. There are no of solutions available but they are of large size code. So need a short but effective code.