Allow only half width katakana (Japanese) characters in text box
<p>In my rails app I have a text field like this </p>

<pre><code>&lt;%= f.text_field :guaranter_furigana_name, autocomplete: "off", class: "form-control", placeholder: "例：ヤマダ タロウ（全角カナ）", maxlength: "50",  :disabled =&gt; @disabled_field %&gt;
</code></pre>

<p>I am using jQuery to validate this text field accepts only <a href="https://en.wikipedia.org/wiki/Half-width_kana" rel="nofollow">half width katakana</a> characters <strong>(Unicode values from ff60 to ff9f)</strong>. This is what I tried</p>

<pre><code>// Allow only half width kana for guranter フリガナ
$(function (){
  $("#t_user_name_register_guaranter_furigana_name").on("input", function(e){

    var key = e.keyCode || e.charCode;

    var inp = String.fromCharCode(key);

    var kanaregexp = new RegExp('[\uff00-\uff9f]');
    if (kanaregexp.test(inp) != true){
      if( key != 8 &amp;&amp; key != 46 &amp;&amp; key != 32){
        inp = $(this).val();
        $(this).val(inp.slice(0,-1));
        $("#telno_errmsg").html("半角カナ字のみを入力してください。").show().fadeOut(3000);
        return false;
      }
    }
  });
});
</code></pre>

<p>The code is a mess and not working always. Please help</p>