Javascript records the number of characters in the text box to detect the number of characters

Source: Internet
Author: User

Javascript records the number of characters in the text box to detect the number of characters

I recently encountered such a problem in a project. I want to count the text entered by users in the text box and display it below, because we are working on an SMS sending platform, if your text message service in our country contains more than 70 characters, the text message will be sent to you in two ways. Therefore, we need to show the user the number of words he entered. So that the user can know how many pieces of information will be provided.

I put a code on the Internet and started using it. It was okay, but I found the problem after using it for a while. That is, after you delete the text, there is a problem with the number of words in the text above. Later, I studied it and found the problem. OnKeyDown = "showLen (this)" onKeyUp = "showLen (this) used only one of the methods each time, and none of them were used, the two methods are used to count when you press the keyboard and release the keyboard. Well, I will put the program up.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">

<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<Meta name = "Generator" CONTENT = "EditPlus">

<Meta name = "Author" CONTENT = "">

<Meta name = "Keywords" CONTENT = "">

<Meta name = "Description" CONTENT = "">

<Mce: script language = "javascript"> <! --

Function showLen (obj)

{

Document. getElementById ("contentLen"). value = obj. value. length;

Document. getElementById ("smsnum"). value = Math. floor (obj. value. length/70) + 1;

}

// --> </Mce: script>

</HEAD>

<BODY>

<Textarea name = "content" cols = "60" rows = "5" id = "content" onKeyDown = "showLen (this)" onKeyUp = "showLen (this)">

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.