整数を受け入れる入力テキストを定義する必要があります。入力フィールドをjQueryで数字だけを受け入れるように設定します。
正規表現を試しましたが、値の小数部分は引き続き表示されています。
私は、この機能を使用:
$(document).on("input","input", function(e) {
this.value = this.value.replace(/[^\d\\-]/g,'');
})
整数を受け入れる入力テキストを定義する必要があります。入力フィールドをjQueryで数字だけを受け入れるように設定します。
正規表現を試しましたが、値の小数部分は引き続き表示されています。
私は、この機能を使用:
$(document).on("input","input", function(e) {
this.value = this.value.replace(/[^\d\\-]/g,'');
})
Finally I solved the above issue with my requirement to type only number is
$(document).on("input", ".skilltxt", function(e) {
this.value = this.value.replace(/[^\d]/g,'');
});
$(".skilltxt").on('paste',function(e) {
if(gBrowser=='IE') {
var clipboardData, pastedData;
clipboardData = e.clipboardData || window.clipboardData;
pastedData = clipboardData.getData('Text');
}
else if((gBrowser=='Firefox')|| (gBrowser=='Chrome')){
var pastedData = (e.originalEvent || e).clipboardData.getData('text/plain');
window.document.execCommand('insertText', false, pastedData)
}
if(Math.floor(pastedData) == pastedData && $.isNumeric(pastedData)){
$(this).val($(this).val().replace(/[^\d]/g,''));
}
else{
return false;
}
});
シンプルで使いやすいです。試してみてください: 編集:貼り付けを防ぐためにonblur
イベントでも電話してください。
function numOnly(selector){
selector.value = selector.value.replace(/[^0-9]/g,'');
}
<input type="text" onkeyup="numOnly(this)" onblur="numOnly(this)">
数値で、あなたが数値の場合、pasteイベントをチェックし、すべての非数値を削除することができます場合は、に検出することができます。これもドットを削除します。
オリジナルソース:keydown detect:Paste remove
以下このコードは、元のソースから変更されています。
てみてください:この約1
$("#input").on("paste", function() {
setTimeout(function(){
if ($('#input').val() != $('#input').val().replace(/\D/g,""))
{
$('#input').val($('#input').val().replace(/\D/g,""));
}
},10)
});
$('#firstrow').on('keydown', '#input', function(e){-1!==$.inArray(e.keyCode,[46,8,9,27,13])||/65|67|86|88/.test(e.keyCode)&&(!0===e.ctrlKey||!0===e.metaKey)||35<=e.keyCode&&40>=e.keyCode||(e.shiftKey||48>e.keyCode||57<e.keyCode)&&(96>e.keyCode||105<e.keyCode)&&e.preventDefault()});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="firstrow">
<input id="input" type="text">
</div>
どのように?
$('input').on('input blur paste', function(){
$(this).val($(this).val().replace(/\D/g, ''))
})
<input>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
なぜタイプ番号を使用していませんか? – guradio
でも入力に10進数(。)を使用していますが、入力は必要ありませんでした(。)。 – durga