jquery Gets the code for the character length in the text box _jquery

Source: Internet
Author: User
Tags pow
When I write an example, two people answered.
I've just tried it, look at the example.
JS Method:
Copy Code code as follows:

<script type= "Text/javascript" >
Gets the true length of the string (two bytes converted to two single-byte)
function Getstractuallen (schars)
{
Schars.replace (/[^\x00-\xff]/g, "xx"). length/1024+ "Byte";
Math.Round (Schars.replace (/[^\x00-\xff]/g, "xx"). length/1024); this looks bad.
Return alert (Formatnum (Schars.replace (/[^\x00-\xff]/g, "xx"). length/1024,4));
}
Format decimals and rounded. such as: Formatnum (100.12345678,4)
function Formatnum (num1,num2) {
if (isNaN (NUM1) | | isNaN (Num2)) {
return (0);
}else{
Num1=num1.tostring ();
Num2=parseint (NUM2);
if (Num1.indexof ('. ') ==-1) {
return (NUM1);
}else{
var b=num1.substring (0,num1.indexof ('. ') +NUM2+1);
var c=num1.substring (Num1.indexof ('. ') +num2+1,num1.indexof ('. ') +NUM2+2);
if (c== "") {
return (b);
}else{
if (parseint (c) <5) {
return (b);
}else{
Return (Math.Round (parsefloat (b) *math.pow (10,num2)) +math.round (parsefloat (Math.pow). ToString (). SUBSTRING (0,math.pow (0.1,num2). toString (). IndexOf ('. ') +num2+1)) *math.pow (10,num2)))/math.pow (10,num2));
}
}
}
}
}

jquery Method:
Copy Code code as follows:

function Getstractuallen () {
var count=$ ("#sChars"). Val (). length/1024;
Return Math.Round (Count*math.pow (10,4));
}

jquery Intercept String length
jquery can use the text () method to get the string, and then length to determine how long, and then use the substring () method format truncated after the ' ... ' ellipsis.
Copy Code code as follows:

<! DOCTYPE HTML Public '-//w3c//dtd XHTML 1.0 transitional//en ' Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ' >
<meta http-equiv= ' content-type ' content= ' text/html; charset=gb2312 '/>
<title>cookie</title>
<script src= ' http://img.jb51.net/jslib/jquery/jquery-1.3.2.min.js ' type= ' Text/javascript ' ></script>
<style>
*{margin:0 padding:0;font-family: ' Arial ', Arial, Helvetica, Sans-serif;
#best {width:300px; height:200px; border:1px solid #ccc; margin:60px auto 0; line-height:1.6; font-size:14px X 0 0 10px}
. blank{font-size:18px; font-weight:bold text-align:center; padding:20px}
</style>
<script type= ' Text/javascript ' >
Jquery.fn.limit=function () {
var self = $ (' div[limit] ');
Self.each (function () {
var objstring = $ (this). text ();
var objlength = $ (this). Text (). length;
var num = $ (this). attr (' limit ');
if (Objlength > num) {
$ (this). attr (' title ', objstring);
Objstring = $ (this). Text (objstring.substring (0,num) + ' ... ');
}
})
}
$ (function () {
$ (document.body). limit ();
})
</script>
<body>
<div class= ' blank ' > Please press F5 to refresh the page .... </div>
<div id= ' best ' >
<div limit= ' ' > String string String Strings string character string string string string </div>
<div limit= ' > String string String Strings string string </div>
<div limit= ' > string string string String </div>
<div limit= ' ' > String string String Strings string literal string string </div>
<div limit= ' > String string String Strings string string </div>
</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.