中文不能输入超过20,英文输入不能超过40个
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script>
function check(){
var newsTitle=document.getElementById("newsTitle").value;
//表单验证
if(getBLen(newsTitle)>40){
alert("标题不能大于40个字符!中文=20,英文40个");
return;
}
}
function getBLen(str){
if (str == null) return 0;
if (typeof str != "string"){
str += "";
}
return str.replace(/[^x00-xff]/g,"01").length;
}
</script>
</head>
<body>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script>
function check(){
var newsTitle=document.getElementById("newsTitle").value;
//表单验证
if(getBLen(newsTitle)>40){
alert("标题不能大于40个字符!中文=20,英文40个");
return;
}
}
function getBLen(str){
if (str == null) return 0;
if (typeof str != "string"){
str += "";
}
return str.replace(/[^x00-xff]/g,"01").length;
}
</script>
</head>
<body>
<input type="text" id="newsTitle" onblur="check();" style="width:800px;height:100px;"/>
</body>
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 数组定义的三种方式
- 下一篇: PHP 统计实时统计汉字个数和区别