JS Limit text Box input length two restrictions (length, number of bytes) _ Basic knowledge

Source: Internet
Author: User
Features/ features
1. Real-time display can enter the number of words (bytes)
2. Two restrictions (length, number of bytes)
3. Chinese input method can be used normally, no bugs
4. The same page can be used multiple, do not interfere with each other
Limit.js
Copy Code code as follows:

function limit () {
var txtnote;//text Box
VAR txtlimit;//hint Word input
The number of words in the Var limitcount;//limit
Whether the Var isbyte;//uses the byte length limit (1 kanji = 2 characters)
When the Var txtlength;//reaches the limit, 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 of each entry
Txtbyte=txtnote.value.replace (/[^/x00-/xff]/g, "xx"). Length;
}
}

Page calls:
Copy Code code as follows:

<body>
<input id= "Txtnote"/>
You can also enter <input type= "text" id= "Txtcount"/> Characters
</body>
<mce:script type= "Text/javascript" ><!--
var lim=new limit ();
Lim.txtnote=document.getelementbyid ("Txtnote");
Lim.txtlimit=document.getelementbyid ("Txtcount");
lim.limitcount=20;
Lim.isbyte=true;
Lim.init ();
--></mce:script>


text box Limits character length
Copy Code code as follows:

<input onkeydown=if (event.keycode==13) event.keycode=9 onkeyup= "Value=value.replace (/[^0-9-]/g,"); "MaxLength= >

The above can only limit the input must be the number and the maximum length of 11 characters how to limit must be entered 11 bits can not be less or more
------Solution--------------------------------------------------------
Are you sure it's the right one? How does the test pass?
No, it's not the test. It's just restricting input. Must be numbers and 11 characters
------Solution--------------------------------------------------------
Copy Code code as follows:

<input onkeydown=if (event.keycode==13) event.keycode=9 onkeyup= "Value=value.replace (/[^0-9-]/g,"); "MaxLength= >

The above can only limit the input must be the number and the maximum length of 11 characters how to limit must be entered 11 bits can not be less or more
------Solution--------------------------------------------------------
Copy Code code as follows:

<input onkeydown=if (event.keycode==13) event.keycode=9 onkeyup= "Value=value.replace (/[^0-9-]/g,"); "MaxLength= >

The above write can only limit the input must be the number and maximum length of 11 characters how to limit ...
------Solution--------------------------------------------------------
Copy Code code as follows:

if (document.form.mobile.value== "")
{
Alert ("You entered an error");
Document.forma.mobile.focus ();
return false;
}
Else
{
if (!/^\d{11}$.test (Document.form.mobile.value))
{Alert ("The number of digits you entered is not correct");
Document.forma.mobile.focus ();
return false;
}
}

I don't know if I can do this.
------Solution--------------------------------------------------------
HTML Code
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> control the number of characters in textarea </title>
<style>
<!--
form{
padding:0px;
margin:0px;
FONT:14PX Arial;
}
input.txt{/* text box individually set * *
border:1px inset #00008B;
Background-color: #ADD8E6;
}
input.btn{/* Button individually set * *
Color: #00008B;
Background-color: #ADD8E6;
border:1px outset #00008B;
padding:1px 2px 1px 2px;
}
-->
</style>
<script language= "JavaScript" >
function LessThan (Otextarea) {
Returns whether the number of characters in a text box is a Boolean value of the symbol requirement
Return OTextArea.value.length < Otextarea.getattribute ("MaxLength");
}
</script>
<body>
<form method= "POST" name= "MyForm1" action= "addinfo.aspx" >
<p><label for= "Name" > Please enter your name:</label>
<input type= "text" name= "name" id= "name" class= "TXT" value= "name" Maxlength= "Ten" ></p>
<p><label for= "Comments" > I want to leave a message:</label><br>
<textarea name= "Comments" id= "comments" "cols=" rows= "4" maxlength= "onkeypress=" return LessThan (this); " ></textarea></p>
<p><input type= "Submit" Name= "Btnsubmit" id= "btnsubmit" value= "Submit" class= "BTN" >
<input type= "reset" name= "btnreset" id= "btnreset" value= "reset" class= "BTN" ></p>
</form>
</body>

------Solution--------------------------------------------------------
HTML Code
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title> Phone number detection </title>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "Dsmart" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
<body>
Phone number: <input id = "Phone" type= "value" onblur= "Checkphone ()"/>
<script type= "Text/javascript" >
function Checkphone () {
var phone = document.getElementById ("Phone"). Value;
if (phone = = "") {
Alert ("Please enter Number");
}else if (!) ( /^\d{11}$/g.test (phone)) {//Limit input 11 integers
}else if (!) ( /^13\d{9}$/g.test (phone) | | (/^15[0-35-9]\d{8}$/g.test (phone)) | | (/^18[05-9]\d{8}$/g.test (phone))) {
Used to detect whether the phone number entered by the user is correctly validated 13 series and 150-159 (excluding 154), 180, 185, 186, 187, 188, 189, length 11 digits
Alert ("Please enter 11 integers");
}else{
Alert ("OK");
}
}
</script>
</body>

------Solution--------------------------------------------------------
Copy Code code as follows:

<input onkeydown=if (event.keycode==13) event.keycode=9 onkeyup= "Value=value.replace (/[^0-9-]/g,"); "MaxLength= >

The above write can only limit the input must be the number and maximum length of 11 characters how to limit ...
This can be a hint, but did not play a limiting role, when the input is less than 11 bits or can be submitted, this is not to use JS to control it?

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.