[pure JS] form の value を半角から全角に変換

by yuta takuman

HTML

<label>input</label></br>
<input name="phone" type="text" placeholder="09000000000" style="width:200px" onchange="han2zen(this)"></br>
<label>textarea</label></br>
<textarea onchange="han2zen(this)" style="width:200px">
</textarea>

JavaScript

function han2zen(obj){
  // 文字列かチェック
  if(typeof(obj.value)!="string") return false;
  var word = obj.value;

  obj.value = word.replace(/[!"#$%&'()*+,\-.\/0-9:;<=>?@A-Z\[\\\]^_`a-z{|}~]/g, function(s) {
    return String.fromCharCode(s.charCodeAt(0) + 0xFEE0);
  });
}