在论坛中看到的,并记录了下来,利用正则的思路值得借鉴
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Test</title>
- </head>
- <script type="text/javascript">
- function filter(e) {
- var v = e.value;
- e.value = v.replace(/[^/w/u4e00-/u9fa5]/g, '');
- if(v.length != e.value.length) {
- document.getElementById('info').innerHTML = '只接受 A-Z,a-z, 0-9,_ 以及中文';
- } else {
- document.getElementById('info').innerHTML = '';
- }
- }
- </script>
- <style type="text/css">
- span#info {
- font-size: 11pt;
- color: red;
- margin-left: 10px;
- }
- </style>
- <body>
- <form name="frm" action="#">
- Value: <input type="text" name="txt" onkeyup="filter(this)" onblur="filter(this)"><span id="info"></span>
- </form>
- </body>
- </html>