Similar to Sina Tencent Weibo's word count statistics control js (distinguishing Chinese and English characters)

Source: Internet
Author: User

<Script>
Var maxstrlen= 140;

Function Q (s ){
Return document. getElementById (s );
}


Function checkWord (c ){
Len = maxstrlen;
Var str = c. value;
MyLen = getStrleng (str );
Var wck = Q ("wordCheck ");
If (myLen> len * 2 ){
C. value = str. substring (0, I + 1 );
}
Else {
Wck. innerHTML = Math. floor (len * 2-myLen)/2 );
}
}
Function getStrleng (str ){
MyLen = 0;
I = 0;
For (; (I <str. length) & (myLen <= maxstrlen * 2); I ++ ){
If (str. charCodeAt (I)> 0 & str. charCodeAt (I) <128)
MyLen ++;
Else
MyLen + = 2;
}
Return myLen;
}
</Script>
</Head>
<Body>
<Div style = "font-size: 16px">
Control the character input in the input box, calculate the total number of input characters, and display the remaining words. <br> one English character is counted as one character, and one Chinese character is counted as two characters.
</Div>
<Div>
<Textarea onKeyUp = "javascript: checkWord (this );"
OnKeyDown = "javascript: checkWord (this);" name = "content" id = "content"
Style = "overflow-y: scroll"> </textarea>
</Div>
<DIV>
You can also enter <span style = "font-family: Georgia; font-size: 26px ;"
Id = "wordCheck"> 140 </span> characters
</Div>
</Body>

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.