Based on jQuery, only numbers (decimal and integer) can be entered in the text box. jquery text box

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.