正则替换非法字符的一个思路

来源:互联网 发布:unity3d vuforia 教程 编辑:程序博客网 时间:2024/04/28 20:46
在论坛中看到的,并记录了下来,利用正则的思路值得借鉴
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.   <title>Test</title>
  6. </head>
  7. <script type="text/javascript">
  8. function filter(e) {
  9.   var v = e.value;
  10.   e.value = v.replace(/[^/w/u4e00-/u9fa5]/g, '');
  11.   if(v.length != e.value.length) {
  12.     document.getElementById('info').innerHTML = '只接受 A-Z,a-z, 0-9,_ 以及中文';
  13.   } else {
  14.     document.getElementById('info').innerHTML = '';
  15.   }
  16. }
  17. </script>
  18. <style type="text/css">
  19. span#info {
  20.   font-size: 11pt;
  21.   color: red;
  22.   margin-left: 10px;
  23. }
  24. </style>
  25. <body>
  26.   <form name="frm" action="#">
  27.     Value: <input type="text" name="txt" onkeyup="filter(this)" onblur="filter(this)"><span id="info"></span>
  28.   </form>
  29. </body>
  30. </html>
 
原创粉丝点击