私は入力として数値を入力したいので、数字の入力を使用しているプロジェクトがありますが、これも小数点の区切り文字を使用できます。,
.
とe
の使用をどのように防止できますか?数字入力のみ
私は周りを見回し、希望の結果を得たものを見つけることができず、10進入力をブロックしなかったか、またはユーザーが引き続きフィールドに貼り付けることができるようになりました。
私は使用できるjavascriptとjqueryを持っていますが、私は外部librairyを使いたくありません。
誰かが私を助けることができるなら、それはappriciatedとなるでしょう。
function LimitKeys($id) {
$field = document.getElementById($id);
$value = $($field).val(); //if the value contains multiple e , or . the value is no longer valid and gives me blank
console.log($value);
$regex = /[^\d]/;
$val = $value.replace($regex, "");// cant replace in blank
console.log($val);
//$($field).val($val);
}
$('.number-only').keypress(function (event) { //sinds it's on keypress it won't catch the paste of data.
console.log(this.value);
var regex = new RegExp("^[0-9]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
試してみてください:$( '。数字のみ')。入力(function(){this.value = this.value.replace(/ \ D/g、 '')} ' –
@ Liam数字0-9と私はそれはそれほど珍しい質問ではないので、前に尋ねられていると仮定しますが、私は検索でそれを見つけられませんでした、または私のニーズを十分に満たしていない答えを持っています – maam27