Use javascript to determine if the string is a positive Integer

HTML:

<input type=”text” size=”1″ onKeyPress=”onlynumbers(this.value);” onMouseOut=”onlynumbers(this.value);”></input>

javascript:
//if the string includes non-number character, isNaN() will return true. Just note that the code “parseInt() == NaN” isn’t right here.
var str_value = 0;
function onlynumbers(str_value) {
  if (isNaN(parseInt(str_value,10))) {
    alert(‘Warning: it’s not a positive integer。’); 
  } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *