How to check for numbers only (including negative)?

how can I check for numbers only from -10 negative to +10 positive?

This is what I have, but I think it's not safe:

if(isset($_POST['number']) && ctype_digit($_POST['number']) && $_POST['number']>=-10 && $_POST['number']<=10){
    //do something
}

and the form:

Input a number between -10 and 10: <input type="text" name="number" size="5" />

Answers


if( isset($_POST['number'])) {
    $num = intval($_POST['number']);
    if( $num >= -10 && $num <= 10) {
        // do something
    }
}

There are other ways, but that one will work. Anything that can't be converted to a number will be treated as zero. If this is not desired behaviour, add:

&& "".$num == $_POST['number']

To that inner IF statement, to ensure that no non-numeric characters were removed from the input.


Check whether a variable is a number including zero and negative values

$x = '-22';
if (isNumber($x, ['zero','negative']))
    echo 'Yes';
else
    echo 'No';

isNumber($x, $includes=[])
{       
    if (is_int($x)) {
        if ($x === 0) {
            if (in_array('zero', $includes))
                return true;
        } elseif ($x < 0) {
            if (in_array('negative', $includes))
                return true;
        } else
            return true;
    } elseif (is_string($x)) {
        if ($x == '0') {
            if (in_array('zero', $includes))
                return true;
        } elseif ($x[0] == '-') {
            if (in_array('negative', $includes))
                return ctype_digit(substr($x, 1));
        } else
            return ctype_digit($x);
    }
}

Need Your Help

Adding javascript and css to Magento

jquery css magento

i am new to Magento i wanted to add custom CCS and jQuery to the body of home page , how can i do that ,please some one explain the procedure step by step or can provide me with links?