0

I'm trying to show an alert if the value of the input (inp_val) isn't between minvalue and maxvalue but it doesn't work, it shows the alert even if the value is between minvalue and maxvalue.

function compare(e){
    this_id = e.id.slice(10,15);
    inp_val = e.value;
    minvalue = document.getElementById('min_' + this_id).value;
    maxvalue = document.getElementById('max_' + this_id).value;
    console.log(maxvalue);
    console.log(minvalue);
    console.log(inp_val);
    if(inp_val < minvalue){
        alert('Minimum Value: ' + minvalue + 'Input Value: ' + inp_val);
    }
    if(inp_val > maxvalue){
        alert('Maximum Value: ' + maxvalue + ' Input Value: ' + inp_val);
    }
}

I'm calling the function compare with an onBlur():

echo '<input class="result_fields" autocomplete="off" onBlur="compare(this)" id="result_inp' . $idc . '" type="text" name="test_res' . $idc . '" />'

I've tried to do this in jQuery but is the same error. Minvalue and maxvalue are input type hidden and its values are stored in a DB on MySQL and retrieved with php like this:

echo '<input type="hidden" value="' . $val_test . '" name="test_name' . $idc . '" id="test_name' . $idc . '" class="testN" />';

The php variable $idc is an incremental function $idc++.

5
  • 1
    use parseInt(x) on all your values ( string values).... Commented Apr 5, 2013 at 20:21
  • 1
    @RoyiNamir: Don't forget the radix parseInt(x, 10) :-P Commented Apr 5, 2013 at 20:23
  • @RocketHazmat yup, (not needed when "use strict") Commented Apr 5, 2013 at 20:30
  • 1
    Also , it seems that chrome (26) already handle this. parseInt('010')=10 and parseInt(010)=8. Commented Apr 5, 2013 at 20:34
  • It's working! I didn't knew about parseInt(), thanks. Commented Apr 5, 2013 at 20:39

3 Answers 3

1

You need to parse your integers or floats from the values you get from the text box

if they are integers:

minvalue = document.getElementById('min_' + this_id).value;
maxvalue = document.getElementById('max_' + this_id).value;
minvalue = parseInt(minvalue,10);
maxvalue = parseInt(maxvalue,10);

Or if they are floats:

minvalue = document.getElementById('min_' + this_id).value;
maxvalue = document.getElementById('max_' + this_id).value;
minvalue = parseFloat(minvalue);
maxvalue = parseFloat(maxvalue);
Sign up to request clarification or add additional context in comments.

2 Comments

You should really use parseInt(minvalue, 10).
There is the Unary Operator Plus that can help too...
1

You need to apply < and > operators on integer value so parse string value to integer and then compare.

if(parseInt(inp_val,10) < parseInt(minvalue,10)){
        alert('Minimum Value: ' + minvalue + 'Input Value: ' + inp_val);
    }

1 Comment

You should really use parseInt(inp_val, 10).
0

Try this:

if(!(inp_val >= minvalue && inp_val <= maxvalue)){
     alert('Max Val: ' + maxvalue + ' Min Val: ' + minvalue + ' Input Value: ' + inp_val);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.