I have to define an input text which accepts only integer numbers.
I tried with a regular expressions but the decimal part of the value is still visible.
I used this function:
$(document).on("input","input", function(e) {
this.value = this.value.replace(/[^\d\\-]/g,'');
})
I have to define an input text which accepts only integer numbers.
I tried with a regular expressions but the decimal part of the value is still visible.
I used this function:
$(document).on("input","input", function(e) {
this.value = this.value.replace(/[^\d\\-]/g,'');
})
Share
Improve this question
edited Oct 26, 2016 at 9:15
Matteo Baldi
5,82811 gold badges44 silver badges53 bronze badges
asked Oct 26, 2016 at 7:55
durgadurga
111 gold badge1 silver badge4 bronze badges
3
- why not use type number? – guradio Commented Oct 26, 2016 at 7:56
- but it is also accepting decimal(.) in input, I didn't required (.) in input. – durga Commented Oct 26, 2016 at 7:58
- Possible duplicate of How to allow only numeric (0-9) in HTML inputbox using jQuery? – emkey08 Commented Jul 18, 2019 at 8:11
4 Answers
Reset to default 3How about this one?
$('input').on('input blur paste', function(){
$(this).val($(this).val().replace(/\D/g, ''))
})
<input>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Simple and easy to use. Try it:
edited: also call in onblur
event to prevent paste.
function numOnly(selector){
selector.value = selector.value.replace(/[^0-9]/g,'');
}
<input type="text" onkeyup="numOnly(this)" onblur="numOnly(this)">
You can detect on keydown
if is numeric and you can check on paste event if is numeric and remove all non-numeric. This remove dot too.
Original source : keydown detect : Paste remove
This code below has been modified from the original source.
Please try:
$( "#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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="firstrow">
<input id="input" type="text">
</div>
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;
}
});