<input readonly='readonly' id ='testid' name='testname' type='number' min ='1'>
I tried this one but not working. is there any way to do it? like using css or javascript.
my jsfiddle.
<input readonly='readonly' id ='testid' name='testname' type='number' min ='1'>
I tried this one but not working. is there any way to do it? like using css or javascript.
my jsfiddle.
This is from Disable writing in input type number HTML5, but it requires jQuery.
$("[type='number']").keypress(function (evt) {
evt.preventDefault();
});
Without jQuery code is: (input is an input field)
input.onkeypress=function(evt){
evt.preventDefault();
};
hello I have a working code but i think this is not the best way. I merged the answer from
How to disable backspace if anything other than input field is focused on using jquery
and the answer of TuomasK
here is the working code.
http://jsfiddle.net/vta96mp1/1/
HTML
<input class='testInput' id ='testid' value = '1' name='testname' type='number' min ='1'>
JS
$("#testid").keypress(function (evt) {
evt.preventDefault();
});
$(document).keydown(function(e) {
var elid = $(document.activeElement).hasClass('textInput');
console.log(e.keyCode + ' && ' + elid);
//prevent both backspace and delete keys
if ((e.keyCode === 8 || e.keyCode === 46) && !elid) {
return false;
};
});
Try this one. It works for me.
$("#testid").keypress(function (e) {
e.preventDefault();
}).keydown(function(e){
if ( e.keyCode === 8 || e.keyCode === 46 ) {
return false;
}
});
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user is pressing a key in the input field.</p>
<input type="text" id="demo0">
<input type="text" id="demo1">
<input type="text" id="demo2" onkeypress="myFunction2()">
<script language="javascript" type="text/javascript">
document.getElementById("demo0").onkeypress = function() {
myFunction0()
};
document.getElementById("demo1").addEventListener("keypress", myFunction1);
function myFunction0() {
document.getElementById("demo0").style.backgroundColor = "yellow";
}
function myFunction1() {
document.getElementById("demo1").style.backgroundColor = "green";
}
function myFunction2() {
document.getElementById("demo2").style.backgroundColor = "red";
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>No text & number</h1>
<div>
<input id="ESP" type="number" name="ESP" onKeypress="event.preventDefault();">
</div>
</body>
</html>