Jquery determines the number of input characters (numbers are counted as 1 in English, Chinese as 2, and are automatically truncated when the length is exceeded)

Source: Internet
Author: User
Tags truncated

1. $ ('textarea # txtPrizeNote '); // specifies the name of the textarea control.
2. The 'span 'tab displays the remaining words.
HTML:
Copy codeThe Code is as follows:
<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>

Copy codeThe Code is as follows:
<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>

Related Article

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.