js得到文本框值判断是否为汉字和数字 求一段代码 判断文本框中的值中数字有多少 汉字有多少..然后相加禁止使用for 谢谢咯 非常感谢 感谢咯..... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 a="不1会2写3作a业b就c来d问e下f我4!";nlength = a.length -a.replace( /\d/g, '').length;clength = ((c=escape(a.replace(/%u/g,''))).length-c.replace(/%u/g,'').length)/2;document.write( nlength+' '+clength);输出: 4 11说明呀,我没有完全解决你的问题的!clength 会是包含 中文字,全角英数符号,所有unicode的字符!还没有处理unicode字符中的只是汉字部分!不能满足的话,将要调用windows输入法类库,也只能在windows下的浏览器执行的了! length 就可以想得到汉字个数就这样<html xmlns="http://www.w3.org/1999/xhtml" ><head><title>标题页</title></head><body><script language="JavaScript"> function cal(str){ re=/[\u4E00-\u9FA5]/g; //测试中文字符的正则 if(re.test(str)) //使用正则判断是否存在中文 return str.match(re).length //返回中文的个数 else return 0 } </script> <input onblur="alert(cal(this.value))"></body> </body></html> var a = "不1会2写3作a业b就c来d问e下f我4", reg = /[[\u4e00-\u9fa5]/g, bytes = a.match(reg).length + a.length; alert(bytes); document.getElementById('id').value().length... js的 string的 length方法会认为汉字也是1个字符,所以你直接用.length就能拿到字符串的长度 <body><input value='测试ab67' id='aa' type='text' /><input type='button' value='check' onclick='check()'/><script> function check() {alert(document.getElementById('aa').value.length);}</script> </body> 你是想要字符数还是字节数?字符和字节都搞不清楚的。。想要字符数直接用length: var str = "测试ab67"; alert(str.length);想要字节数就看6楼 就是汉字占两个字节.然后数字字母占一个字节var a = "不1会2写3作a业b就c来d问e下f我4",reg = /[[\u4e00-\u9fa5]/g,bytes = a.match(reg).length + a.length;alert(bytes);这段代码算对总数了呀,傻瓜! 你一定要分开两个变量就改改嘛!var a = "不1会2写3作a业b就c来d问e下f我4",singlebytes = a.match( /[[\u0000-\u00ff]/g, '').length;doublebytes = a.length-singlebytes;alert('双字节的字节数:'+(doublebytes*2)+' 单字节:'+singlebytes); 获取div焦点 动态显示层 一个表单怎么提交给两个页面? 怎样把字符串"1234#1234#4567#"转换为三个数字1234,1234,4567 急需两张图片交换的代码 急求一份javascript写的日历代码,同时支持IE和netscape,能传值,快救命阿,马上交了! 看看这个有什么错误?应该怎么改? iframe问题 【textarea】按下回车发送消息不换行,怎么实现? js鼠标经过显示隐藏DIV,鼠标移开隐藏DIV如何继续隐藏? easy ui 点击tab标签刷新内容,ie中不能用。火狐没问题 一个比较偏的javascript问题,求大神。
nlength = a.length -a.replace( /\d/g, '').length;
clength = ((c=escape(a.replace(/%u/g,''))).length-c.replace(/%u/g,'').length)/2;document.write( nlength+' '+clength);输出: 4 11说明呀,我没有完全解决你的问题的!
clength 会是包含 中文字,全角英数符号,所有unicode的字符!
还没有处理unicode字符中的只是汉字部分!不能满足的话,将要调用windows输入法类库,也只能在windows下的浏览器执行的了!
想得到汉字个数就这样<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>标题页</title>
</head>
<body>
<script language="JavaScript">
function cal(str)
{
re=/[\u4E00-\u9FA5]/g; //测试中文字符的正则
if(re.test(str)) //使用正则判断是否存在中文
return str.match(re).length //返回中文的个数
else
return 0
}
</script>
<input onblur="alert(cal(this.value))"></body>
</body>
</html>
reg = /[[\u4e00-\u9fa5]/g,
bytes = a.match(reg).length + a.length;
alert(bytes);
<input value='测试ab67' id='aa' type='text' />
<input type='button' value='check' onclick='check()'/>
<script>
function check() {alert(document.getElementById('aa').value.length);}
</script>
</body>
想要字符数直接用length: var str = "测试ab67"; alert(str.length);
想要字节数就看6楼
var a = "不1会2写3作a业b就c来d问e下f我4",
reg = /[[\u4e00-\u9fa5]/g,
bytes = a.match(reg).length + a.length;
alert(bytes);
这段代码算对总数了呀,傻瓜! 你一定要分开两个变量就改改嘛!
var a = "不1会2写3作a业b就c来d问e下f我4",
singlebytes = a.match( /[[\u0000-\u00ff]/g, '').length;
doublebytes = a.length-singlebytes;alert('双字节的字节数:'+(doublebytes*2)+' 单字节:'+singlebytes);