How to force Input field to enter numbers only using JavaScript ? Last Updated : 16 Sep, 2024 Comments Improve Suggest changes Like Article Like Report We are going to learn how can we force the Input field to enter numbers only using JavaScript. Below are the approaches to force input field force numbers using JavaScript:Table of ContentBy using ASCII ValueBy using replace(), isNan() functionBy using ASCII ValueIn this approach, we are going to use ASCII code for taking numeric values from the input fields.We will check if the given input value does not lie between the numeric ASCII code then we will not accept that value.If the value lie between those numeric ASCII value then we will accept the input value.Example: This example is the implementation of the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> div { width: 400px; height: 130px; border: 2px solid black; padding: 15px; position: absolute; left: 27%; } </style> </head> <body> <center> <h1 style="color:green;padding:13px;"> GeeksforGeeks </h1> <h3> Force input field to take only numbers as an input </h3> </center> <div class="container"> <form name="inputnumber" autocomplete="off"> <b>Register No:</b> <input type="text" onkeypress="return onlyNumberKey(event)" maxlength="11" size="50%" /> <br> <br> <b>Ph. Number:</b> <input type="tel" size="50%" onkeypress="return onlyNumberKey(event)" /> <br> <br> <center> <b>Age:</b> <input type="number" placeholder=" Only 12+ allowed" min="12" /> <input type="submit" value="Submit" onclick="return detailssubmit()"> </center> </form> </div> <script> function detailssubmit() { alert("Your details were Submitted"); } function onlyNumberKey(evt) { // Only ASCII character in that range allowed let ASCIICode = (evt.which) ? evt.which : evt.keyCode if (ASCIICode > 31 && (ASCIICode < 48 || ASCIICode > 57)) return false; return true; } </script> </body> </html> Output:By using replace(), isNan() functionIn this approach, we are taking input from input from input filed having type text.On submission we are chechking whether the value is valid a valid number or not and rendering response accrodingly.Also, we are replacing the given value by reducing it to only numeric value.Example: This example is the implementation of the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <center> <h1 style="color:green;padding:13px;"> GeeksforGeeks</h1> <h3> Input[type=text] allow only Numeric Value Example </h3> <br> <form autocomplete="off" id="form1"> <b>Enter 4-digit Pin:</b> <input type="text" name="numonly" maxlength="4"> <br> <br> <b>Enter passkey:</b> <input type="text" name="passkey" id="num" oninput="return onlynum()" minlength="2"> </form> </center> <script> $(function () { $("input[name='numonly']").on('input', function (e) { $(this).val($(this).val().replace(/[^0-9]/g, '')); }); }); function onlynum() { let fm = document.getElementById("form2"); let ip = document.getElementById("num"); let tag = document.getElementById("value"); let res = ip.value; if (res != '') { if (isNaN(res)) { // Set input value empty ip.value = ""; // Reset the form fm.reset(); return false; } else { return true } } } </script> </body> </html> Output:Â Comment More infoAdvertise with us Next Article How to force Input field to enter numbers only using JavaScript ? V VigneshKannan3 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Restrict Input to Numbers and Decimals in JavaScript? Restricting an input box to allow only numbers and decimal points involves setting up validation rules that ensure users can only enter numerical values and decimal separators. This helps maintain data integrity and prevents invalid entries in forms or user input fields.Below are the approaches to r 3 min read How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery? Given an HTML document containing <input type = "number"> attribute and the task is to disable the mouse scroll wheel to change the number value using JavaScript/jQuery. Approach: Select the element.Add an event to that element which will call a function when scrolling starts.Make the element 2 min read How to allow only 10 numbers in a textbox using jQuery ? In this article, we will learn about how to allow only 10 numbers in a textbox using jQuery, jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, Given an HTML document containing a textbox input element, the task is to allow the input of strictl 4 min read Create OTP Input Field using HTML CSS and JavaScript We will build an OTP (One-Time Password) input box, which is commonly used on many websites. This interactive and engaging UI element allows users to enter their OTP conveniently and efficiently. We will walk through the step-by-step process of creating this functionality using HTML, CSS, and JavaSc 3 min read Get the Value of Text Input Field using JavaScript Getting the value of a text input field using JavaScript refers to accessing the current content entered by a user in an input element, such as <input> or <textarea>, by utilizing the value property. This allows for dynamic interaction and data manipulation in web forms.Syntax inputField 2 min read How to Validate Decimal Numbers in JavaScript ? Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N 2 min read How to Validate an Input is Alphanumeric or not using JavaScript? To validate alphanumeric in JavaScript, regular expressions can be used to check if an input contains only letters and numbers. This ensures that the input is free from special characters.Approach: Using RegExpA RegExp is used to validate the input.RegExp is used to check the string of invalid chara 1 min read How to display a message when given number is between the range using JavaScript ? In this article, we will learn to display a message when a number is between the given range using JavaScript. We take a number from the user and tell the user whether it is in between a range or not, in our case, the range is 1 to 10. Approach: We create a button and add a click event listener to i 1 min read Build a Spy Number Checker using HTML CSS and JavaScript In the realm of mathematics, Spy Numbers, also known as secretive numbers or cryptic numbers, possess a unique property. A spy number is defined as a number whose sum of digits is equal to the product of its digits. In this article, we will explore how to build a Spy Number Checker using HTML, CSS, 3 min read How to validate if input in input field has float number only using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only float numbers are allowed i.e. there not allowed 5 min read Like