In jquery, you can use the replace() method and regular expressions to set that only numbers can be filled in. The replace() method is used to replace a substring that matches the regular expression. When the filled content is not a number, it will not be filled in. Display, the syntax is "specify element object.val().replace(/[^\d]/g,"")".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How to set jquery to only fill in numbers
Some input text boxes need to make some judgments. Only numbers can be entered. This judgment can be passed Regular expressions are used to judge. When the content entered by the user is not a number, it will not be displayed in the input text box. Let us take a look at how jq can set only numbers to be entered.
Syntax:
$('input').on('input propertychange', function(e) { var text = $(this).val().replace(/[^\d]/g, ""); $(this).val(text) })
Create a new html file.
Create an input box on the html file, and then set the size of the input box.
Code:
Introduce the jquery.min.js file.
Code:
Use regular expressions to determine users Every time you enter content, it will not be displayed if it is not a number.
Code:
<script> $(function(){ $(&#39;input&#39;).on(&#39;input propertychange&#39;, function(e) { var text = $(this).val().replace(/[^\d]/g, ""); $(this).val(text) }) }) </script>
Picture:
After saving the html file, open it in a suitable browser for testing, and it is found that the input is not a number The content cannot be displayed on the text box. As shown in the picture:
# Just copy and paste all the code into the new html file, and you can see the effect after saving.
All codes:
<script> $(function(){ $(&#39;input&#39;).on(&#39;input propertychange&#39;, function(e) { var text = $(this).val().replace(/[^\d]/g, ""); $(this).val(text) }) }) </script>
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of How to set jquery to only fill in numbers. For more information, please follow other related articles on the PHP Chinese website!

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

Key goals and advantages of HTML5 include: 1) Enhanced web semantic structure, 2) Improved multimedia support, and 3) Promoting cross-platform compatibility. These goals lead to better accessibility, richer user experience and more efficient development processes.

The goal of HTML5 is to simplify the development process, improve user experience, and ensure the dynamic and accessible network. 1) Simplify the development of multimedia content by natively supporting audio and video elements; 2) Introduce semantic elements such as, etc. to improve content structure and SEO friendliness; 3) Enhance offline functions through application cache; 4) Use elements to improve page interactivity; 5) Optimize mobile compatibility and support responsive design; 6) Improve form functions and simplify verification process; 7) Provide performance optimization tools such as async and defer attributes.

HTML5transformswebdevelopmentbyintroducingsemanticelements,multimediacapabilities,powerfulAPIs,andperformanceoptimizationtools.1)Semanticelementslike,,,andenhanceSEOandaccessibility.2)Multimediaelementsandallowdirectembeddingwithoutplugins,improvingu

TherealdifferencebetweenusinganIDversusaclassinCSSisthatIDsareuniqueandhavehigherspecificity,whileclassesarereusableandbetterforstylingmultipleelements.UseIDsforJavaScripthooksoruniqueelements,anduseclassesforstylingpurposes,especiallywhenapplyingsty


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
