限制Text-Area域中的字符的个数

<textarea id="mytextarea"></textarea>

<script>
jQuery.fn.maxLength = function(max){
    this.each(function(){
        var type = this.tagName.toLowerCase();
        var inputType = this.type? this.type.toLowerCase() : null;
        if(type == "input" && inputType == "text" || inputType == "password"){
            //应用标准的maxLength
            this.maxLength = max;
        }else if(type == "textarea"){
            this.onkeypress = function(e){
                var ob = e || event;
                var keyCode = ob.keyCode;
                var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
                return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
            };
            this.onkeyup = function(){
                if(this.value.length > max){
                    this.value = this.value.substring(0,max);
                }
            };
        }
    });
};
//use
$(‘#mytextarea‘).maxLength(10);
</script>
时间: 2024-07-28 16:18:32

限制Text-Area域中的字符的个数的相关文章

统计字符串中每个字符的个数

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>统计字符串中每个字符的个数@</title> 6 </head> 7 <body> 8 </body> 9 10 <script type="text/javascript"&

将字符串中不同字符的个数打印出来

描述:  找出字符串中,不同的字符的个数. 题目类别:  字符串  难度:  初级  运行时间限制: 无限制 内存限制: 无限制 阶段:  入职前练习  输入: 输入一个字符串,'\0'作为字符串结束符. 输出: 输出字符串中不同字符的个数. 样例输入: 122345 样例输出: 5 完整代码: #include <iostream> using namespace std; int main() { int cnt[256]={0}; char s[100]; int count=0; ge

2015华为机试——将字符串中不同字符的个数打印出来

描述: 找出字符串中,不同的字符的个数. 题目类别: 字符串 难度: 初级 运行时间限制: 无限制 内存限制: 无限制 阶段: 入职前练习 输入: 输入一个字符串,'\0'作为字符串结束符. 输出: 输出字符串中不同字符的个数. 样例输入: 122345 样例输出: 5 代码如下: public class dayin_Char { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while

统计字符串中各类字符的个数

//输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. public class lianxi07 { public static void main(String[] args) { int digital = 0; int character = 0; int other = 0; int blank = 0;      char[] ch = null;      Scanner sc = new Scanner(System.in);      String s = sc

转载:js实现统计字符串中特定字符出现个数的方法

//js统计字符串中包含的特定字符个数 function getPlaceholderCount(strSource) {   //统计字符串中包含{}或{xxXX}的个数   var thisCount = 0;   strSource.replace(/\{[xX]+\}|\{\}/g, function (m, i) {     //m为找到的{xx}元素.i为索引     thisCount++;   });   return thisCount; }

统计字符串中指定字符的个数

输入一个字符串和一个字符,统计这个字符在字符串中出现的次数 输入格式: 输入2行.第1行是字符串,第2行是要查找的字符. 输出格式: 字符出现的次数 输入样例: abcdefgabcdefg a 输出样例: 2 a=input() b=input() def CountAa(s): return s.lower().count(b) if __name__ == "__main__": s = a print(CountAa(s)) 原文地址:https://www.cnblogs.c

统计一行字符中各类字符的个数

/*输出一行字符,分类统计字符个数*/ #include<stdio.h>#include<stdlib.h>int main(void){ int letter=0,space=0,digit=0,other=0;/*定义变量并初始化*/ char c;/*定义字符串c*/ printf("请输入一行字符,以回车键结束\n"); while((c=getchar())!='\n')/*判断c是否是回车键*/ if(c>='a'&&c<

C#:统计字符串中每个字符的个数

具体代码如下: 1 namespace demo 2 { 3 public class Program 4 { 5 static void Main(string[] args) 6 { 7 string str = "hi,good morning."; 8 Console.WriteLine($"字符串:{str}"); 9 Dictionary<char, int> dic = Count(str); 10 foreach(var c in dic

统计字符串中某个字符的个数

private int countNum(final String str, final char ch){ int num = 0; if (str != null){ for(char i: str.toCharArray()){ if(i==ch){ ++num; } } } return num; } 原文地址:https://www.cnblogs.com/wenlin-gk/p/9849679.html