JS Judgment String byte length (automatic recognition in English and Chinese)

Source: Internet
Author: User

You will use the early evening, using CSS to control the length of the text is not what is required to meet, now backstage also regardless of character length interception, can only use JS to intercept the string.

The code is as follows Copy Code

/**
* Returns the byte length of the character (2 bytes of Chinese characters)
* @param {string}
* @returns {Number}
*/

var Getbytelen = function (val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (Val[i].match (/[^x00-xff]/ig)!= null)//Full angle
Len + 2;
Else
Len + 1;
};
return Len;
}
var SABC = ' 1a ah Ah 22 Fly 3 place a ';
var ol = Getbytelen (SABC);
Alert (' byte length obtained directly with length: ' + sabc.length ');
Alert (' The byte length obtained through the Getbytelen () method: ' + ol ');

You can continue to write a new string that intercepts the specified length by using the above method. Complement:

  code is as follows copy code

/**
* Returns the byte length of the character (2 bytes of Chinese characters)
* @param {String}{number}
* @returns {string} + ' ... '
*/

var cutstrfornum = function (str, num) {
var len = 0;
for (var i = 0; i < str.length; i++) {
if (Str[i].match (/[^x00-xff]/ig)!= null)//Full angle
Len + 2;
Else
Len + 1;
}
if (len >= num) {
Newstr = str.substring (0, num) + "...";
}
return newstr;
}
var SABC = ' 1a ah Ah 22 Fly 3 place a ';
Alert (Cutstrfornum (SABC, 3));

Look at an example JS judge the number of characters entered


1. $ (' textarea#txtprizenote '); Represents the name of the textarea control

2. ' Span ' shows the label of the remaining words

Html:

The code is as follows Copy Code

<div>
<textarea id= "Txtprizenote" runat= "Server" height= "74px" width= "480px" maxlength= "10"
Style= "width:480px; height:74px; Float:left "></textarea>
<span style= "color:red;" >*</span><br/>
Remaining words: <span id= "showmsg" style= "color:red" ></span>
</div>


<script type= "Text/javascript" >
Returns the byte length of Val
Function Getbytelen (val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (Val[i].match (/[^x00-xff]/ig)!= null)//Full angle
Len + 2;
Else
Len + 1;
}
return Len;
}

       //Returns the value of Val within the specified byte length max
         Function Getbyteval (val, max) {
            var returnvalue = ';
            var bytevallen = 0;
            for (var i = 0; i < val.length i++) {
&nbs p;               if (Val[i].match/[^ X00-XFF]/IG)!= null)
                     Bytevallen + 2;
                Else
                     Bytevallen + + 1;

if (Bytevallen > Max)
Break

ReturnValue + = Val[i];
}
Return returnvalue;
}

        $ (function () {
             var _area = $ (' textarea#txtprizenote ');
            var _info = _area.next ();
            var _max = _area.attr (' maxlength ');
            var _val;
            _area.bind (' keyup change ', function () {// Bind KeyUp and Change events
                if (_info.find (' span '). Size () < 1) {//avoid inserting a hint message every time you bounce
                     _info.append (_max);
               }

                _val = $ (this). Val ( );
                _cur = Getbytelen ( _val);

                if (_cur = 0) {// When the default value length is 0 o'clock, the number of entries is the default maxlength value
                     _info.text (_max);
               } else if (_cur < _max) {////When the default value is less than the limit, the number of max-cur
                     _info.text (_max-_cur);
               } else {//
                When the default value is greater than or equal to the limit      _info.text (0);

                     $ (this). Val (Getbyteval (_val,_max)); Intercepts the value in the specified byte length
               }
           });
       });
    </script>

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.