You can use the setInputFilter function to install an input filter:
setInputFilter(document.getElementById("myTextBox"), function(value) { return /^\d*\.?\d*$/.test(value); // Allow digits and '.' only, using a RegExp. }, "Only digits and '.' are allowed");
This function will restrict the input values of a text to the given inputFilter function. It supports Copy Paste, Drag Drop, keyboard shortcuts, context menu operations, non-typeable keys, the caret position, different keyboard layouts, validity error messages, and all browsers since IE 9.
Note: You must still do server-side validation for security!
The above is the detailed content of How Can I Restrict an HTML Text Input to Only Accept Numeric Values (and a Decimal Point)?. For more information, please follow other related articles on the PHP Chinese website!