Home  >  Article  >  Web Front-end  >  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
WBOYOriginal
2016-05-16 16:47:241449browse

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
})

Statement:
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