有时由于用户的原因导致数据库的数据与当前输入状态不统一造成的错误,引发用户对系统的质疑,所以我们在处理数据的要有一个统一的标准来使我们的系统更加稳定,一般情况下都是全角转半角。
前端校验
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> p{float: left;} </style> </head> <body> <p id="p1"></p> <p>=>|转化半角后|=></p> <p id="p2"></p> </body> </html> <script type="text/javascript"> var str = "12345"; function toSemiangle(s){ var result = ''; for (i=0 ; i<s.length; i++){ code = s.charCodeAt(i);//获取当前字符的unicode编码 if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符 { result += String.fromCharCode(s.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码 }else if (code == 12288)//空格 { result += String.fromCharCode(s.charCodeAt(i) - 12288 + 32); }else { result += s.charAt(i); } } return result; } document.getElementById('p1').innerHTML = str; document.getElementById('p2').innerHTML = toSemiangle(str); </script>
