Input Only Valid Numbers (Improved)

[Total: 0    Average: 0/5]

This shows how to have a number (valid with decimal point – only 1).

Body Code:

<script language="javascript"> 
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var IS_PERIOD=46;
var PERIOD_TYPED=false;
function onlyDigits(e) {
  var _regExp=/./;
  PERIOD_TYPED=document.exf1.tinput.value.match(_regExp);
  var _ret = true;
  if (isIE) {
    if (window.event.keyCode == IS_PERIOD) {
	  if (!PERIOD_TYPED) {
	    PERIOD_TYPED=true;
	  } else {
	    window.event.keyCode=0;
	    _ret = false;
	  }
	}
    if (window.event.keyCode < 46 || window.event.keyCode > 57) {
      window.event.keyCode = 0;
      _ret = false;
    }
  }
  if (isNS) {
    if (e.which == IS_PERIOD) {
	  if (!PERIOD_TYPED) {
	    PERIOD_TYPED=true;
	  } else {
	    e.which=0;
	    _ret = false;
	  }
	}
    if (e.which < 46 || e.which > 57) {
      e.which = 0;
      _ret = false;
    }
  }
  return (_ret); 
}
</script> 
<form name=exf1>
<input type=text name="tinput"> 
</form>
<script>
if (isNS) document.captureEvents(Event.KEYPRESS);
document.onkeypress=onlyDigits;
</script> 

Times Viewed: 0

Leave a Reply

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