1 <script type="text/javascript">
2 /**
3 * 字符串求长度(全角)
4 */
5 String.prototype._getLength = function() {
6 var str = this;
7 var len = str.length;
8 var reLen = 0;
9 for (var i = 0; i < len; i++) {
10 if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
11 // 全角
12 reLen += 2;
13 } else {
14 reLen++;
15 }
16 }
17 return reLen;
18 }
19 function test(){
20 alert(document.getElementById("inputDemo").value._getLength());
21 }
22 </script>
23
24 <input type="text" id="inputDemo" />
25 <input type="button" value="click me!" onclick="test()"/>