0

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.

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
Humaira
  • 3
  • 2

4 Answers4

0

<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...

Parth Raval
  • 4,097
  • 3
  • 23
  • 36
0

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;
}
Schien
  • 3,855
  • 1
  • 16
  • 29
  • I do have a button for calling out my function. But do I need to validate using this code separately for all the degree, minutes and seconds of both latitude and longitude? – Humaira Apr 27 '18 at 05:32
  • I have edited the answer to reflect multiple fields. If you need to ensure the proper format of a lat-lng input, you probably want to use a regular expression instead of enforcing integers in separate fields. – Schien Apr 27 '18 at 05:40
  • It's not working. – Humaira Apr 27 '18 at 05:45
  • Which part is unexpected? Any error messages? – Schien Apr 27 '18 at 05:46
  • No, but it's still accepting decimal values to give the output. – Humaira Apr 27 '18 at 05:48
  • In your main function, call checkvalues: if (!checkvalues()) return false; – Schien Apr 27 '18 at 05:49
0

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

Glyphack
  • 876
  • 9
  • 20
0

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();
                    }
                }
            }
        });
});
Mohammad Raheem
  • 1,131
  • 1
  • 13
  • 23