Home >Web Front-end >JS Tutorial >js limits text box input length in two ways (length, number of bytes)_Basic knowledge

js limits text box input length in two ways (length, number of bytes)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:46:092222browse
Function/Features:
1. Real-time display of the number of words that can be entered (number of bytes)
2. Two restriction methods (length, number of bytes)
3. Chinese input method It can be used normally without BUG
4. You can use multiple ones on the same page without interfering with each other
limit.js
Copy Code The code is as follows:

function limit(){
var txtNote;//Text box
var txtLimit;//Input prompt for the number of words
var limitCount;//Limited number of words
var isbyte;//Whether to use byte length limit (1 Chinese character = 2 characters)
var txtlength;//When the limit is reached, the length of the string
var txtByte;
this.init=function(){
txtNote=this.txtNote;
txtLimit=this.txtLimit;
limitCount=this.limitCount;
isbyte=this.isbyte;
txtNote.onkeydown=function(){wordsLimit()};txtNote.onkeyup=function(){wordsLimit()};
txtLimit.value=limitCount;
}
function wordsLimit(){
var noteCount=0;
if(isbyte){noteCount=txtNote.value.replace(/[^/x00-/xff]/g,"xx").length}else{noteCount=txtNote.value .length}
if(noteCount>limitCount){
if(isbyte){
txtNote.value=txtNote.value.substring(0,txtlength Math.floor((limitCount-txtByte)/2)) ;
txtByte=txtNote.value.replace(/[^/x00-/xff]/g,"xx").length;
txtLimit.value=limitCount-txtByte;
}else{
txtNote.value=txtNote.value.substring(0,limitCount);
txtLimit.value=0;
}
}else{
txtLimit.value=limitCount-noteCount;
}
txtlength=txtNote.value.length;//Record the length after each input
txtByte=txtNote.value.replace(/[^/x00-/xff]/g,"xx").length;
}
}

Page call:
Copy code The code is as follows:




You can also entercharacters





Text box limited character length
Copy code The code is as follows:



Writing like this can only limit The input must be numbers and the maximum length is 11 characters. How to limit it? You must enter 11 digits, no less or more
------Solution--------------- ------------------------------------------
Are you sure the above is written correctly? ?. . . Why can't I pass the test? .
Isn’t it right? There is something wrong in the test. Now it only limits the input to numbers and 11 characters
------Solution------------------- -------------------------------------
Copy code The code is as follows:



The above writing can only limit the input to numbers and the maximum length is 11 How to limit the characters? You must enter 11 characters, no less or more
------Solution-------------------------- -------------------------------
Copy code The code is as follows:



The above can only limit the input to numbers and the maximum length is 11 characters. How to limit...
------Solution----------------------------------------- ---------------
Copy code The code is as follows:

if(document.form.mobile.value=="")
{
alert("오류를 입력하셨습니다.")
document.forma.mobile.focus();
false 반환
}
else
{
if(!/^d{11}$.test(document.form.mobile.value))
{alert(" 당신 입력한 자릿수가 잘못되었습니다.");
document.forma.mobile.focus();
return false;
}
}

안해요 이것이 작동하는지 모르겠습니다. . .
------해결책----------------------------- ----- ------------------
HTML 코드
코드 복사 코드는 다음과 같습니다.




텍스트 영역의 문자 수 제어

<스크립트 언어 = javascript ");
}




;input type="text" name="name" id ="name" class="txt" value="Name" maxlength="10">