Share Blog

Saturday, June 30, 2018

Allow only numeric (0-9) Or Numeric With Decimal in HTML inputbox using jQuery


How to allow only numeric (0-9) Or Numeric With Decimal in HTML inputbox using jQuery?


I am creating a web page where I have an input text field in which I want to allow only numeric characters like (0,1,2,3,4,5...9) 0-9.


<script>
$(document).ready(function () {
//Allow Number Only in Textbox
$(".allownumber").on("keypress keyup blur", function (event) {
$(this).val($(this).val().replace(/[^\d].+/, ""));
if ((event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
//Allow Number Only in Textbox

//Allow Number With decimal Only in Textbox
$(".allownumberwithdecimal").on("keypress keyup blur", function (event) {
//this.value = this.value.replace(/[^0-9\.]/g,'');
$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
//Allow Number With decimal Only in Textbox
});
</script>



<span>Float</span>
<input type="text" name="numeric" class='allownumberwithdecimal'>
<div>Numeric values only allowed (With Decimal Point) </div>
<br/> <br/> <br/>
<span>Int</span>
<input type="text" name="numeric" class='allownumber'>
<div>Numeric values only allowed (Without Decimal Point) </div>



No comments:

Post a Comment