Home > Web Front-end > JS Tutorial > JS controls the length of the string in the input box_javascript skills

JS controls the length of the string in the input box_javascript skills

WBOY
Release: 2016-05-16 16:47:24
Original
1462 people have browsed it

Copy code The code is as follows:

// Get the byte length of the string
function len(s) {
s = String(s);
return s.length (s.match(/[^x00-xff]/g) || "").length;// plus Matched full-width character length
}

function limit(obj, limit) {
var val = obj.value;
if (len(val) > limit) {
val=val.substring(0,limit);
while (len(val) > limit){
val = val.substring(0, val.length - 1);
};
obj.value = val;
}
}

$("#nickName").keyup(function(){
limit(this,20);//Within 20 bytes
})

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template