Limit the number of text bytes in form. js code_form special effect
Source: Internet
Author: User
JavaScript code that limits the number of text bytes in form /*
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:
Or
*/
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;
}
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.