Between Two Numbers

Question: If I want validate a number such that it should be compared with two values i.e. maximum and minimum values, how do I do it?

Answer: Here is the Javascript that you would use to perform such a validation:

var min=3;
var max = 10;
var num = parseInt(myform.numfield.value);
if (min > num || max < num) {
alert(num + ' is not between ' + min + ' and ' + max);
return false;

Simply change the values for min and max to the boundary values that are acceptable and change the myform.myfield reference to use the actual names of the form and field to be validated.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow