开发者

Count characters in field dynamically

开发者 https://www.devze.com 2023-02-19 04:47 出处:网络
I would like to count characters in a form textarea field dynamically, I kno开发者_如何学Gow there is a similar question about this but I am wondering if it can count from a certain number towards 0,

I would like to count characters in a form textarea field dynamically, I kno开发者_如何学Gow there is a similar question about this but I am wondering if it can count from a certain number towards 0, as example while writing a tweet on Twitter.

This code could be in javascript, PHP, it doesn't really matter to me.


This should help you out. Here is a tutorial regarding usage - a demo is found below:

Usage:

<input name="text" onKeyDown="CountLeft(this.form.text, this.form.left,50);"
                   onKeyUp="CountLeft(this.form.text,this.form.left,50);">

Javascript:

<SCRIPT LANGUAGE="JavaScript">

 function CountLeft(field, count, max) 
 {
     if (field.value.length > max)
         field.value = field.value.substring(0, max);
     else
         count.value = max - field.value.length;
 }

</SCRIPT>

Demo - Credit to www.reconn.us

Hope this helps.


With Javascript, use the KeyUp event handler, and check for the legnth-property on your text field on every event.

Example: http://jsfiddle.net/nslr/C2CNS/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号