The difference between the string length calculation function is that numbers are recorded as 1 in English and Chinese as 2. This function is suitable for computer character encoding principles. See some examples below.
You will use CSS to control the length of text later. After all, it is not enough to meet any requirement. Now, no matter whether the character length is intercepted in the background, you can only use JS to intercept strings.
The Code is as follows: |
Copy code |
/** * Returns the length of characters in bytes (two 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) // fullwidth Len + = 2; Else Len + = 1; }; Return len; } Var sAbc = '1a Ah 22 fly 3 places '; Var ol = getByteLen (sAbc ); Alert ('byte length obtained using length directly: '+ sAbc. length ); Alert ('length of bytes obtained through the getByteLen () method: '+ ol ); |
With the above method, you can continue to write a new string with the specified length... Completion:
The Code is as follows: |
Copy code |
/** * Returns the length of characters in bytes (two 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) // fullwidth Len + = 2; Else Len + = 1; } If (len> = num ){ NewStr = str. substring (0, num) + "..."; } Return newStr; } Var sAbc = '1a Ah 22 fly 3 places '; Alert (cutStrForNum (sAbc, 3 )); |
Check instance JS to determine the number of characters entered
1. $ ('textarea # txtPrizeNote '); // specifies the name of the textarea control.
2. The 'span 'tab displays 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"> // Return 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) // fullwidth Len + = 2; Else Len + = 1; } Return len; } // Return the value of val in the specified byte length max. Function getByteVal (val, max ){ Var returnValue = ''; Var byteValLen = 0; For (var I = 0; I <val. length; I ++ ){ 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 the keyup and change events If (_ info. find ('span '). size () <1) {// avoid inserting a prompt message every time a bullet occurs. _ Info. append (_ max ); } _ Val = $ (this). val (); _ Cur = getByteLen (_ val ); If (_ cur = 0) {// when the default value is 0, you can enter the default value maxlength. _ Info. text (_ max ); } Else if (_ cur <_ max) {// when the default value is smaller than the limit, the number of inputs can be 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); // intercept the value within the specified byte length } }); }); </Script> |