Based on jQuery, only numbers (decimal and integer) can be entered in the text box. jquery text box
In practical use, sometimes only integers can be input in the text box, but sometimes it may be more "friendly", and floating point numbers can be input, the following example shows how to use jquery to implement a text box that can only input decimals. The Code is as follows:
<! DOCTYPE html>
The above Code meets our requirements. Only integers or floating-point numbers can be entered in the text box. The code is relatively simple and will not be described here.
The following describes how to use jquery to implement a text box that can only input integers:
Sometimes you may need to specify that the content of the text box can only be an integer. The following code example can be provided for your reference.
The Code is as follows:
<! DOCTYPE html>
The above Code meets the expected requirements. Only an integer can be entered in the text box. The following describes the implementation process.
Code comment:
1. $ (function () {}). When the document structure is fully loaded, run the code in the function.
2. $ (". NumText"). keyup (function () {}), register the keyup event handler function for the text box.
3. $ (this ). val ($ (this ). val (). replace (/\ D | ^ 0/g, ''). Use the replace () function to replace non-numeric content with null values using a regular expression.
4. bind ("paste", function () {$ (this ). val ($ (this ). val (). replace (/\ D | ^ 0/g, '');}), register the paste event to handle ambiguity. Of course, the chained call is used here, it can prevent users from using ctrl + v to copy and paste.
The above code is relatively simple to write, some of the difficulties are provided with comments, I believe it will help you.
Articles you may be interested in:
- Jquery-based setting page text box can only enter the implementation code of numbers
- Jquery-limited text box can only enter numbers, that is, integers and Decimals
- In the jquery tutorial, only numbers and decimal point examples can be entered in the restricted text box.
- Three Jquery methods to restrict text boxes from entering only numbers and letters