Here are a couple functions I keep using to validate numbers.
Is a number whole?
Code:
function isWhole(x)
{
var number = x;
var rounded = Math.round(number);
if (number == rounded)
{
//What do do if the number is whole
}
else
{
//What to do if the number is not whole
//or the number is not a number
}
}
Just replace the commented lines with useful code.
I find it extremely useful to use something like return "The number was whole.";
This way, you can use <? echo 'var input = '.$_GET['input'].';'; ?>
var result = isMultiple(input);
To call this function, use:
Is a number a multiple of x?
This is a little trickier.
Code:
function isMultiple(y,z)
{
var dividee = y;
var divider = z;
var divided = dividee / divider;
var dividem = Math.round(divided);
if (divided == dividem)
{
//What do do if the number is a multiple
}
else
{
//What to do if the number is not a multiple
//or the number is not a number
}
}
Do the same with the commented code.
To call this one, use:
Code:
isMultiple(number,is_a_multiple_of_?)
(is_a_multiple_of_? should be the number to check if number is a multiple of.)
Bookmarks