[JS] Code that uses JavaScript regular expressions to limit the number of text bytes

Source: Internet
Author: User

Write a piece of Js for you when you are working on the project. The limit on the number of characters in text is frequently used in actual development. The main problem is the limit on Chinese characters. Code This solves the issue of limiting the number of bytes for verification. You only need to save the code to a JS file and introduce it to the verification page! At the same time, I hope you will give us great support and valuable comments. I will give you more excellent comments in the future. Article Thank you !!

The following is a reference clip:
/*
Value: value;
Bytelength: Database Byte Length
Title: Chinese name of the field
Attribute: attribute name
Usage instructions:

Add (1) onkeyup = "limitlength (this. Value, 100, 'name', 'name ')"
(2) id = "name" or [struts tag] styleid = "name"
Note: The ID and attribute names must be the same.

Example: <textarea name = "Explain" id = "Explain" onkeyup = "limitlength (value, 5, 'semantic description', 'explain ')">

Or

<Input type = "text" name = "Explain" id = "Explain" onkeyup = "limitlength (value, 5, 'semantic description', 'explain ')">

*/Copy codeThe Code is as follows:

Function limitlength (value, bytelength, title, attribute ){
VaR newvalue = value. Replace (/[^ \ x00-\ xFF]/g ,"**");
VaR length = newvalue. length;

// When the number of bytes entered is smaller than the set number of bytes
If (length * 1 <= bytelength * 1 ){
Return;
}
VaR limitdate = newvalue. substr (0, bytelength );
VaR COUNT = 0;
VaR limitvalue = "";
For (VAR I = 0; I <limitdate. length; I ++ ){
VaR flat = limitdate. substr (I, 1 );
If (flat = "*"){
Count ++;
}
}
VaR size = 0;
VaR istar = newvalue. substr (bytelength * 1-1, 1); // check whether the checkpoint is "×"

// If the base point is X; determines whether X is an even number or an odd number within the base point.
If (count % 2 = 0 ){
// When the value is an even number
Size = count/2 + (bytelength * 1-count );
Limitvalue = value. substr (0, size );
} Else {
// When the value is an odd number
Size = (count-1)/2 + (bytelength * 1-count );
Limitvalue = value. substr (0, size );
}
Alert (Title + "maximum input" + bytelength + "bytes (equivalent to" + bytelength/2 + "Chinese characters )! ");
Document. getelementbyid (attribute). value = limitvalue;
Return;
}

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.