Only Number Input Validation in Javascript

To restrict input to only numbers in JavaScript, you can use the <input> element with the type attribute set to "number". Additionally, you can add some JavaScript to handle input validation if needed.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Number Input Restriction</title>
    <script>
        function allowOnlyNumbers(event) {
            const input = event.target;
            input.value = input.value.replace(/[^0-9]/g, '');
        }
    </script>
</head>
<body>
    <input type="text" oninput="allowOnlyNumbers(event)" />
</body>
</html>

administrator

Leave a Reply

Your email address will not be published. Required fields are marked *