全角字符一半用于排版等,在编程中通常是禁止使用的。而用户错误的输入(全角,占两个字符),很容易超出限长或者给用户带来错觉。所以有必要把全角转半角。
<html> <head> <script language="javascript" type="text/javascript"> /**************************** *参数说明: * 全角->半角 * str:要转换的字符串 * 返回值类型:字符串 ***************************/ function DBC2SBC(obj) { var str = obj.value; var i; var result=""; for(i=0;i<str.length;i++){ code = str.charCodeAt(i); // “65281”是“!”,“65373”是“}” if(code>=65281 && code<65373){ // “65248”是转换码距 result+=String.fromCharCode(code-65248); }else{ result+=str.charAt(i); } } obj.value = result; } </script> </head> <body > <input type="text" id="fname" onblur="DBC2SBC(this);" /> </body> </html>
?
?
效果就不提了,very easy!