js判断字符是否存在汉字的方法

时间:2023-12-17 19:26:26

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <title>js判断字符是否是汉字</title>
  <style type="text/css">
      .content{
          width:350px;
          overflow:hidden;
          border:1px solid #ddd;
      }
  </style>
<script language="javascript" type="text/javascript">
  function CheckChinese(obj,val){    
  var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
  if(reg.test(val)){    
       alert("不能输入汉字!"); 
       var strObj = document.getElementById(obj); 
       strObj.value = ""; 
       strObj.focus();         
  }      
  }
</script>
</head>
<body>
  <div class="content">
      <div>测试字符:<input id="test" type="text" onblur="CheckChinese('test',this.value)" /> </div>
  </div> 
</body>
</html>