入力文字列の中に全角英数字と全角記号(!”#$%&'()=~^‘[@「+*};:」「<>?_、。・)があったらinputBoxのフォーカスが外れたら半角に変換する。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>全角を半角に変換するサンプル</title>
<style>
body {
margin: 20px;
}
h1 {
font-size: 140%;
}
input {
font-size: 120%;
padding: 5px;
width: 400px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function(){
$("#foo").change(function(){
var str = $(this).val();
str = str.replace( /[A-Za-z0-9-!”#$%&’()=<>,.?_[]{}@^~¥]/g, function(s) {
return String.fromCharCode(s.charCodeAt(0) - 65248);
});
$(this).val(str);
}).change();
});
</script>
</head>
<body>
<h1>全角を半角に変換するサンプル</h1>
<div>入力 <input type="text" id="foo" value="" /></div>
<p>フォーカスをはずすとテキストフィールド内の全角英数といくつかの記号を半角文字にします。</p>
</body>
</html>
<使用例>

