I can\'t find the right regex pattern for this, even though there are a lot of questions in this kind.
I dont want the user to be able to type or input
If it comes to HTML5 then you might try min attribute:
<input type="number" min="0">
You can't press ' - ' button on keyboard.
<!--HTML Code is below-->
<input type="text" name="value1" id="value1" class="form-control" required="required" onkeyup="keyCode(event)">
<!--JQuery Code is below-->
<script>
$(document).ready(function() {
$value1.on("keyup", function keyCode(event){
var x = event.keyCode;
if (x == 109 || x == 189) {
event.preventDefault();
alert ("You can't enter minus value !");
}
});
});
</script>
Keycode of '109' represent numeric side minus button of keyboard. Keycode of '189' represent minus button above of the character buttons.
To disallow any input but numeric, you may use
<input type="text" onkeypress="return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 57" name="itemConsumption" />
^------------....
<form id="mfrm">
<table>
<tr>
<td>Enter number only: <input type="text" name="itemConsumption" onkeypress="return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 57" /></td>
<td><input type="Submit"/></td>
</tr>
</table>
Here, the event.charCode == 8 || event.charCode == 0 || event.charCode == 13
condition handles the case when DELETE, BACKSPACE or ENTER keys are pressed (important for Firefox, see Mohit's comment below and datashaman's comment related to enabling the ENTER key).
The event.charCode >= 48 && event.charCode <= 57
means that only 0 (decimal code 48) and all other digits up to 9 (decimal code 57) will be returned.