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
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
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; } }
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------------------- -------------------------------------
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-------------------------- -------------------------------
The above can only limit the input to numbers and the maximum length is 11 characters. How to limit... ------Solution----------------------------------------- ---------------