Home Web Front-end JS Tutorial Code to calculate the number of words in a text box based on jQuery_jquery

Code to calculate the number of words in a text box based on jQuery_jquery

May 16, 2016 pm 05:52 PM
word count text box

1. Function:
1. The calculation is performed while the user is typing, telling the user how many words are left to input;
2. When the specified number of words is exceeded, click OK and the input box will flash
2. Functional Analysis
1. What events are the key points?
Standard browsers use oninput, while IE uses onpropertychange. The conditions for the occurrence of these two events are that the value of the text box changes.
 2. Calculation of word count.
2.1 One Chinese character counts as two, one symbol or number, and English character counts as one. (If 140 characters are specified, multiply by 2, then it will be 280)
2.2 You need to use the Math.ceil method, because in the end you still have to divide by 2 to restore the number of words displayed to the user;
3. Flash Dynamic background color
Modular operation is used here, because it is a repeated action. The first time there is color, and the second time there is no color. Such repeated actions will have a flashing effect.
Because the naked eye needs to see this The secondary effects are color and colorless, so delay, setTimeout and setInterval are needed. SetInterval is used here because the action needs to be repeated.

Thanks to "Wiaowei Classroom" for the video

Online demo: http://demo.jb51.net/js/2012/myinputCount/
Package download: http://www.jb51.net/jiaoben/55149.html


Copy code The code is as follows:

$(function(){
var $tex = $(".tex");
var $but = $(".but");
var ie = jQuery.support.htmlSerialize;
var str = 0;
var abcnum = 0;
var maxNum = 280;
var texts= 0;
var num = 0;
var sets = null;
$tex.val("" );
//Tip text at the top
$tex.focus(function(){
if($tex.val()==""){
$("p"). html("The number of characters you can also enter140");
}
})
$tex.blur(function(){
if($tex. val() == ""){
$("p").html("Please enter your text below:");
}
})
//Number of words in the text box Calculate and prompt changes
if(ie){
$tex[0].oninput = changeNum;
}else{
$tex[0].onpropertychange = changeNum;
}
function changeNum(){
//The number of Chinese characters
str = ($tex.val().replace(/w/g,"")).length;
//Non-Chinese characters Number
abcnum = $tex.val().length-str;
total = str*2 abcnum;
if(str*2 abcnum$but.removeClass()
$but.addClass("but");
texts =Math.ceil((maxNum - (str*2 abcnum))/2);
$( "p").html("You can also enter the number of words" texts "").children().css({"color":"blue"});
} else if(str*2 abcnum>maxNum){
$but.removeClass("")
$but.addClass("grey");
texts =Math.ceil(((str*2 abcnum )-maxNum)/2);
$("p").html("The number of words you entered exceeds " texts "").children("span").css ({"color":"red"});
}
}
//Button click
$but.click(function(){
if($(this).is (".grey")){
sets = setInterval(flash,100);
$tex.addClass("textColor")
}
function flash(){
num;
if(num == 4){
clearInterval(sets);
}
if(num%2 == 1){
$tex.addClass("textColor")
}else{
$tex.removeClass("textColor")
}
}
})
})
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check the word count in Windows 10 Notepad How to check the word count in Windows 10 Notepad Dec 29, 2023 am 10:19 AM

When using Win10 Notepad to input text, many friends want to check how much text they have input. So how to check it? In fact, you can see the number of words by just opening the text properties and checking the byte count. How to check the word count in Notepad in win10: 1. First, after editing the content in Notepad, save it. 2. Then right-click the notepad you saved and select. 3. We see that it is 8 bytes, because the size of each Chinese character is 2 bytes. 4. After we see the total bytes, just divide it by 2. For example, 984 bytes, divided by 2 is 492 words. 5. But it should be noted that each number such as 123 only occupies one byte, and each English word only occupies one byte.

How to set a picture as the background in OneNote How to set a picture as the background in OneNote May 14, 2023 am 11:16 AM

Onenote is one of the best note-taking tools offered by Microsoft. Combined with Outlook and MSTeams, Onenote can be a powerful combination for increasing productivity at work and in personal creative productivity. We have to take notes in a different format, which may be more than just writing things down. Sometimes we need to copy images from different sources and do some editing in our daily work. Images pasted on Onenote can go a long way if you know how to apply the changes. Have you ever encountered a problem when using Onenote that images pasted on Onenote cannot allow you to work easily? This article will look at using images effectively on Onenote. we can

How to hide text until clicked in Powerpoint How to hide text until clicked in Powerpoint Apr 14, 2023 pm 04:40 PM

How to hide text before any click in PowerPoint If you want text to appear when you click anywhere on a PowerPoint slide, setting it up is quick and easy. To hide text before clicking any button in PowerPoint: Open your PowerPoint document and click the Insert menu. Click on New Slide. Choose Blank or one of the other presets. Still in the Insert menu, click Text Box. Drag a text box onto the slide. Click the text box and enter your

How to check word count in word? How to check word count in word How to check word count in word? How to check word count in word Mar 04, 2024 am 10:04 AM

Word is one of the most popular office software. Sometimes there are strict word count requirements for the formatting of documents. For example, the number of words in a title should not be too long. A title that is too long is not eye-catching enough. There are also certain paragraphs that need to be put into some software systems. Too many or too few words will affect the beauty of the layout. If you count the words one by one, it will look dull and a waste of time. You may even count the words incorrectly. How do you check the number of words in Word? Let’s learn several ways to check word count in Word. How to check word count in word? How to check the word count in Word The first method is to use Word word count to check 1. Select the "Review" tab and click "Word Count" to count the number of pages, words, characters, paragraphs, lines, etc. of the document. information. Hold

How to make a calendar in Word How to make a calendar in Word Apr 25, 2023 pm 02:34 PM

How to Make a Calendar in Word Using Tables If you want to create a calendar that fits your specifications exactly, you can do everything from scratch using tables in Word. This allows you to design the exact layout you want for your calendar. Create a calendar using tables in Word: Open a new Word document. Press Enter a few times to move the cursor down the page. Click the Insert menu. In the ribbon, click the table icon. Click and hold the upper left square and drag out a 7×6 table. Write the day of the week on the first line. Use another calendar as a reference to fill in the days of the month. Highlight any date outside the current month. In the main menu, click the text color icon and select Gray. For the current month, start with

What are the types of html text boxes? What are the types of html text boxes? Oct 12, 2023 pm 05:38 PM

HTML text box types include single line text box, password text box, number text box, date text box, time text box, file upload text box, multi-line text box, etc. Detailed introduction: 1. The single-line text box is the most common type of text box, used to accept single-line text input. The user can enter any text in the text box, such as user name, password, email address, etc.; 2. The password text box is used To accept password input, when the user enters the password, the content in the text box will be hidden to protect the user's privacy; 3. Numeric text box, etc.

How to add line breaks in HTML textbox? How to add line breaks in HTML textbox? Sep 04, 2023 am 11:05 AM

To add a newline character to an HTML text area, we can use the HTML newline tag to insert a newline character anywhere. Alternatively, we can also use the CSS property "white-space:pre-wrap" to automatically add line breaks to the text. This is particularly useful when displaying preformatted text in a text area. So let’s discuss ways to add line breaks. Method creates a text area in HTML and assigns it an id. Create a button that, when clicked, will split the text of the text area using line breaks. Now create a function that separates text into newlines. The code of this function is -functionreplacePeriodsWithLineBreaks()

How to set the text box border to be transparent How to set the text box border to be transparent Jul 28, 2023 am 10:05 AM

How to set the text box border to be transparent: 1. Open the document, select the inserted text box, right-click and select "Format Object"; 2. In the pop-up box on the right, select "Shape Options" - "Fill and Line" - "Transparency" ”; 3. Adjust the transparency as needed.

See all articles