Degree <input type="number" name="Degree" min="0" step="1"/ size="4" id="Degreelat">
Want this degree value to take only positive integers and not accept decimal values before calling in my function in javascript.
Degree <input type="number" name="Degree" min="0" step="1"/ size="4" id="Degreelat">
Want this degree value to take only positive integers and not accept decimal values before calling in my function in javascript.
<input type="number" name="Degree" min="0" step="1"/ size="4" id="Degreelat" onkeydown="return ( event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )">
You can validate onKeyDown event javascript by validating & disabling all sorts of navigation and hotkeys... like comma,dot,alphabets...
It's your JavaScript function's responsibility to validate the input. Either you have a button that explicitly calls this function, or an onblur or onchange event that triggers the function, let's look at the function itself:
function checkvalue(){
var value=document.getElementById('Degreelat').value;
var val=parseInt(value,10);
if (val!=value) return false;
if (val<=0) return false;
}
If you want to check multiple fields, you can abstract a helper function and use it to indicate which field is invalid:
function valint(id){
var value=document.getElementById(id).value;
if (!parseInt(value,10)!=value) {
document.getElementById(id).style.background='red';
return false;
}
document.getElementById(id).style.background='transparent';
return true;
}
function checkvalues(){
var valid=true;
if (!valint('Degreelat')) valid=false;
if (!valint('Degreelng')) valid=false;
return valid;
}
A simple solution would be white listing numbers key codes(using Jquery):
$(document).ready(function() {
$("#Deareelat").keydown(function (e) {
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
});
but you might also want to allow some other keys too(eg. backspace) here's a list of key codes:
https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Using Jquery for only positive integers
//for allow only positive integers
$(document).ready(function(){
$("#returnRetailQuantity").keydown(function (event) {
if (event.shiftKey) {
event.preventDefault();
}
if (event.keyCode == 46 || event.keyCode == 8) {
}
else {
if (event.keyCode < 95) {
if (event.keyCode < 48 || event.keyCode > 57) {
event.preventDefault();
}
}
else {
if (event.keyCode < 96 || event.keyCode > 105) {
event.preventDefault();
}
}
}
});
});