In my rails app I have a text field like this
<%= f.text_field :guaranter_furigana_name, autocomplete: "off", class: "form-control", placeholder: "例:ヤマダ タロウ(全角カナ)", maxlength: "50", :disabled => @disabled_field %>
I am using jQuery to validate this text field accepts only half width katakana characters (Unicode values from ff60 to ff9f). This is what I tried
// 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 && key != 46 && key != 32){
inp = $(this).val();
$(this).val(inp.slice(0,-1));
$("#telno_errmsg").html("半角カナ字のみを入力してください。").show().fadeOut(3000);
return false;
}
}
});
});
The code is a mess and not working always. Please help