Use HTC technology to limit the length of multi-line input boxes

Source: Internet
Author: User

/*
Tofu is made of excellent products
Http://www.asp888.net bean curd technology station
If reprinted, Please retain the complete copyright information
*/
We know that the input length of a text input box must be limited sometimes. We can use maxlength to limit the input length of text and password.
The input length of the input box of the type is limited. However, when we use the maxlength attribute for textarea, we are sorry.
This attribute does not work in textarea.
Is there any way? The answer is yes, yes! What Is HTC ?? Simply put, HTC is the HTML component.
The expression capability is not strong. Let's take a look at the example below.

Test.html:
<Form method = "Post">
<P> <input type = "text" size = "30" maxlength = "50" name = "T1">
<Textarea name = "S1" rows = "4" Cols = "30" maxlength = "50" style = "behavior: URL (maxlength. HTC)"> </textarea>
</Form>

We have noticed that we have never seen such a usage before: Style = "behavior: URL (maxlength. HTC)". Let's take a look.
The following HTC content

<Public: component id = "bhvmaxlength" Urn = "maF: maxlength">
<Public: property name = "maxlength"/>
<Public: attach event = "onkeypress" handler = "dokeypress"/>
<Public: attach event = "onbeforepaste" handler = "dobeforepaste"/>
<Public: attach event = "onpaste" handler = "dopaste"/>

<Script language = "jscript">
// Keep user from entering more than maxlength characters
Function dokeypress (){
If (! Isnan (maxlength )){
Maxlength = parseint (maxlength );
VaR OTR = element.doc ument. selection. createRange ();
// Allow user to type character if at least one character is selected
If (OTR. Text. length> = 1)
Event. returnvalue = true;
Else if (value. length> maxLength-1)
Event. returnvalue = false;
}
}
// Cancel default behavior
Function dobeforepaste (){
If (! Isnan (maxlength ))
Event. returnvalue = false;
}
// Cancel default behavior and create a new paste routine
Function dopaste (){
If (! Isnan (maxlength )){
Event. returnvalue = false;
Maxlength = parseint (maxlength );
VaR OTR = element.doc ument. selection. createRange ();
VaR iinsertlength = maxlength-value. Length + OTR. Text. length;
VaR sdata = Window. clipboardData. getdata ("text"). substr (0, iinsertlength );
OTR. Text = sdata;
}
}
</SCRIPT>

</Public: component>

About htc content on the msdn site is very detailed, interested friends can go to http://msdn.microsoft.com/workshop/components/htc/reference/htcref.asp

Author: tofu (original)

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.