
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to Only Allow Numbers in a Text Box using jQuery?
We can easily allow only numbers in a textbox using jQuery. Under the keyup() set the code for allowing only number values so that the textbox never accept strings. We will see two examples −
- Allow Only Numbers in a TextBox using jQuery replace()
- Allow Only Numbers in a TextBox using jQuery fromCharCode()
Allow Only Numbers in a TextBox using jQuery replace()
In this example, we will see how to allow only numbers in a TextBox using jQuery replace(). We must set the code under the keyup to allow only numbers while typing, else the textbox will not allow typing strings −
$('.numbers').keyup(function () { this.value = this.value.replace(/[^0-9\.]/g,''); });
Above, we have set a Regex that only allows number 0-9 to be typed in the textbox. The .numbers is a class set for both the inputs i.e. −
<p>Rank: <input type="text" class="numbers" value="" /> </p> <p>Marks: <input type="text" class="numbers" value="" /> </p>
Example
Let us now see the complete example to allow only numbers in a TextBox using jQuery replace() −
<!DOCTYPE html> <html> <title>Input Numbers Only</title> <head> <script src="https://code.jquery.com/jquery-git.js"></script> </head> <body> <h1>Score</h1> <p>Rank: <input type="text" class="numbers" value="" /> </p> <p>Marks: <input type="text" class="numbers" value="" /> </p> <script> $('.numbers').keyup(function () { this.value = this.value.replace(/[^0-9\.]/g,''); }); </script> </body> </html>
Output

The above textboxes only allow number input.
Allow Only Numbers in a TextBox using jQuery fromCharCode()
In this example, we will allow only numbers in a TextBox using jQuery fromCharCode(). We must set the code under the keypress to allow only numbers while typing, else the textbox will not allow typing strings −
$('.numbers').keypress(function (e) { if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)) return false; });
Above, we have set a Regex that only allows number 0-9 to be typed in the textbox. The .numbers is a class set for both the inputs i.e. −
<p>Rank: <input type="text" class="numbers" value="" /> </p> <p>Marks: <input type="text" class="numbers" value="" /> </p>
Example
Let us now see the complete example to allow only numbers in a TextBox −
<!DOCTYPE html> <html> <title>Input Numbers Only</title> <head> <script src="https://code.jquery.com/jquery-git.js"></script> </head> <body> <h1>Score</h1> <p>Rank: <input type="text" class="numbers" value="" /> </p> <p>Marks: <input type="text" class="numbers" value="" /> </p> <script> $('.numbers').keypress(function (e) { if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)) return false; }); </script> </body> </html>
Output

- Related Articles
- How to replace only text inside a div using jQuery?
- How to Apply Data Validation to Allow Only Numbers in Excel?
- How to get selected text from a drop-down list (select box) using jQuery?
- How to extract only the numbers from a text field in MySQL?
- How to find text box height in IText using FabricJS?
- How to underline all words of a text using jQuery?
- How to Create a Comment Box with a Containing Text Using CSS
- How To Create A Text Inside A Box Using HTML And CSS
- How to input text in the text box without calling the sendKeys() using Selenium?
- How do I display only the visible text with jQuery?
- Text in Transparent Box using CSS3
- How to Allow Only Yes or No Entry in Excel?
- How to change text inside an element using jQuery?
- How to remove underline from text hyperlink using jQuery?
- How to Allow Only Date Format in Specific Cells in Excel?
