prefix 0 (zero)
prefix 0 (zero)
by pj_js15
HTML
Jquery <br />
<input id="txtInput" type="text" />
<br />
<br />
Plain JS <br />
<input id="txtInput2" type="text" onkeypress="GetChar(event);"/>
<script>
//plain JS
function GetChar (event){
console.log(event);
var chCode = ('charCode' in event) ? event.charCode : event.keyCode;
var element = window.event.srcElement.id;//event.target.id - not working in IE 7/8
var curVal = document.getElementById(element).value;
if(chCode == 46){
document.getElementById(element).value = "0";
}
}
</script>
JavaScript
$().ready(function() {
//jquery
$('#txtInput').bind('keypress', function(e) {
if(e.keyCode == 46){
if($('#txtInput').val().indexOf('.') != -1){
e.preventDefault();
}else{
curVal = $('#txtInput').val();
$('#txtInput').val( '0' + curVal );
}
}
});
});