Validating that a required form field is a number

Assuming that the value we are testing is a number, this is a simple if (...) condition testing whether the value is less than 0.

Enter test:

The code:

<script type="text/javascript">
<!--

function isPositiveNumber(field)
{
  var n = field.value;

  if (n < 0)
  {
    alert("This field is required to be a positive number.");
      
    // if you want to change the number without
    // user interaction, put your code here
    
    field.focus();
    field.select();
    return false;
  }
  return true;
}
  
// -->
</script>