入力文字列の中に全角英数字と全角記号(!”#$%&'()=~^‘[@「+*};:」「<>?_、。・)があったらinputBoxのフォーカスが外れたら半角に変換する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!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 > $(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 > |
<使用例>