|
cssで ime-mode: disabled; を指定すればInternet ExplorerやFirefoxは制御できますが、Chromeは言うことを聞いてくれません。 方法1†HTML5ではinput typeに電話番号入力欄を作成するtelが指定できます。 通常はinput typeはtextでime-mode: disabled;としておく。 <style>
#username {
ime-mode: disabled;
}
</style>
<script>
$(function() {
var agent = window.navigator.userAgent.toLowerCase();
if (agent.indexOf('chrome') != -1) {
document.getElementById('username').type = 'tel';
}
});
</script>
<form method="post" action="/">
<input type="text" name="username" id="username">
<input type="submit" value="送信">
</form>
方法2†classにalphanumericを指定し、JavaScriptにて全角から半角へ変換を行います。 $(function() {
$('.alphanumeric').css('ime-mode', 'disabled');
$('.alphanumeric').on('keyup', function(){
var halfVal = this.value.replace(/[!-〜]/g,
function(tmpStr) {
return String.fromCharCode(tmpStr.charCodeAt(0) - 0xFEE0);
});
this.value = halfVal.replace(/”/g, "\"")
.replace(/’/g, "'")
.replace(/‘/g, "`")
.replace(/¥/g, "\\")
.replace(/ /g, " ")
.replace(/〜/g, "~");
});
});
|