How can I limit only the number of inputs in the text box,

Source: Internet
Author: User

How can I limit only the number of inputs in the text box,

How can I limit the number of a text box:
In some cases, the content of a text box can only be entered with numbers, such as mobile phone numbers or ID card numbers. The following describes how to achieve this through an example. The code example is as follows:

 

<! DOCTYPE html> 

 

The above Code meets our needs. When entering non-numeric content in the text box, it is automatically cleared. The following describes the implementation process:
I. Implementation principle:
In the code, use the on () function to register the click event handler for the text box. When you click the text box, you can register the content in the text box and then register the keyup event handler for the text box, when the buttons are released, you can get the content entered in the text box, and then use the replace function and the regular expression matching principle to replace non-numeric elements with null. This is generally the case, for more information, see the following code annotations.
Ii. Code comments:
1. jQuery (document). ready (function ($) {}). After the document structure is fully loaded, run the code in the function.
2. var phone = $ ('# phone'), get the object whose id attribute value is phone.
3. $ (phone). on ('click', function () {}), use on to register the click event handler for the element.
4. phone. val (''), set the value in the text box to null.
5. $ (phone ). on ('keyup', function (evt) {}), use on as the keyup event processing function of the element parent book. The parameter of the function is the event object, which is not used here and can be removed.
6. var phoneVal = phone. val () to get the content of the text box.
7. phoneVal = phoneVal. replace ('/[^ \ d] +/G', ''), replace the non-numeric element in the text box with null.
8. phoneVal = parseInt (phoneVal, 10), convert the number to the numerical type.
9. if (isNaN (phoneVal), check whether the parameter is of non-numerical type.
10. phoneVal = ''. Set the content to null.
11. this. value = phoneVal. Set the content in the text box to the phoneVal after processing.
Iii. Related reading:
1. For more information about the on function, see the on () method section of jQuery.
2. For more information about the val () function, see the val () method section of jQuery.
3. For details about keyup events, refer to jQuery's keyup Events section.
4. For the parseInt () function, see section 1 of parseInt () function in JavaScript.
5. For details about the isNaN () function, refer to isNaN () method 1 in JavaScript.

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 8796.

For more information, see: http://www.softwhy.com/jquery/

 

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.