views:

36

answers:

2

Hi, i have a simple text input field users put in a number. I need to check which range the number matches (e.g. >10 or <1 ) and save a separate key/value for that range in the database.

I thought about a hidden field that changes its value as the user enters data but i don't know how to determine the range.

(I cannot modify the submit button. I use php and jQuery.)

Thanks for any help!

+1  A: 

Relatively simple...

$num = (int) $_POST['num'];
if ($num < 1) {
    // do something
}
elseif ($num > 10) {
    // do something else
}
fire
+1  A: 

If your ranges are fixed and you are asking about client side code then

var ranges = {'range1':{min:-1000,max:2},'range2':{min:3,max:10},'range3':{min:11,max:1000}};

$('#number').keyup(findRange);

function findRange(){
    var num = parseInt( $('#number').val() );
    $('#secret').val('');
    for(rng in ranges)
    {
        if (num >= ranges[rng].min && num <=ranges[rng].max)
        {
            $('#secret').val(rng);
        }
    }
}

Demo: http://www.jsfiddle.net/hbPj9/

Gaby
That's exactly what i needed! Perfect and amazing. Thank you so much!
Christoph