59
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chromeで日本語入力を禁止する

Last updated at Posted at 2014-07-14

##Chromeで日本語入力を禁止したい
・IEやFFと同じようにChromeで日本語の入力を禁止したい
・ChromeはCSSのime-mode:disabledに対応していない
・「とにかくヤレ」と神の声を賜ったので、JavaScriptで頑張ってみる

2015/9/17編集
masa-kunikataさんアドバイスの正規表現方式に変更。
textareaで改行・タブもとおるようにしました。

filter_mbc.js
jQuery(document).ready(function() {
	
    // ime-modeが使えるか
    var supportIMEMode = ('ime-mode' in document.body.style);
	
    // 非ASCII
    var noSbcRegex = /[^\x00-\x7E]+/g;

    // 1バイト文字専用フィールド
	jQuery('.sbc_field')
	.on('keydown blur paste', function(e) { 
		
        // ime-modeが使えるならスキップ
        if (e.type == 'keydown' || e.type == 'blur')
            if (supportIMEMode) return;
		
        // 2バイト文字が入力されたら削除
        var target = jQuery(this);
        if(!target.val().match(noSbcRegex)) return;
        window.setTimeout( function() {
          target.val( target.val().replace(noSbcRegex, '') );
        }, 1);        
        
	});
	
});
test.css
.sbc_field { ime-mode: disabled; }
test.html
<input type="text" value="abcde" class="sbc_field" />

##デモ
Demoはこちら

●確認した環境
Chrome 36b
Internet Explorer 10
Firefox 30
(Windows 7)

●自賛
IMEが有効でもテンキーで数字の入力はできる
(先にevent.keyCodeの監視で書いてました)

●欠点
一瞬チラチラみえてかっこわるい

59
61
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
59
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?