前提・実現したいこと
分単位で入力したら、◯◯時間◯◯分に変換したテキストを入力欄の側にテキストとして表示させる入力フォームを作成しています。入力欄には半角数字以外入力できないようにしたい。
発生している問題・エラーメッセージ
JSでイベント(keydown keyup blur)が発火した時に、全角数字を半角に変換していますが、safariで、全角文字入力→enterを押した時に同じ文字が入力されるなど、挙動がおかしくなる。
該当のソースコード
SAMPLE.ConvertInputTime.prototype = { IS_ERROR: 'err', init: function() { this.$parents = this.$trigger.closest('td'); this.$output = this.$parents.find('.jscConvertTimeOutput'); this.bindEvent(); }, bindEvent: function() { var self = this; this.$trigger.on('keyup keydown blur', function() { self.convert($(this)); }); }, convert: function($trigger) { var maxlength = this.$trigger.attr('maxlength'); var inputValue = $trigger.val(); inputValue = inputValue.replace(/[A-Za-z0-9]/g, function($trigger){ return String.fromCharCode($trigger.charCodeAt(0) - 65248) }).slice(0, maxlength); this.$trigger.val(inputValue); var alertText = this.getAlertText(inputValue); this.changeBgColor(inputValue); this.$output.text(alertText); }, getAlertText: function(inputValue) { var time = Math.floor(inputValue / 60); var minutes = inputValue % 60; if(inputValue <= 0 || inputValue % 5 !== 0 || inputValue === '') { return '5分単位で入力してください'; }else if(time === 0 && minutes < 60) { return minutes + '分'; }else if(minutes === 0) { return time + '時間'; }else { return time + '時間' + minutes + '分'; } }, changeBgColor: function(inputValue){ if(inputValue % 5 !== 0) { this.$parents.addClass(this.IS_ERROR); return; } this.$parents.removeClass(this.IS_ERROR); }, };
試したこと
イベントをkeydown keyupではなくinputにするとchromeで同じように挙動がおかしくなった。
また<input type="number" />はsafariで<input type="text" />と同じ挙動になってしまったので使用できない。
入力制限を最新のPCブラウザ(IE11、edge、firefox、chrome、safari)に対応させることは可能でしょうか?

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/03 04:14
2017/07/03 12:21