2

I'm trying to make a simple calc usign values from input fileds, but I have a problem with passing values to if statement. It looks like this:

$('#confirmSila').click(function(){
var dlugosc = $('#sila').val();
var silaOd = $('.silaOd').val();
var silaDo = $('.silaDo').val();
if ((dlugosc > silaOd) && (dlugosc < silaDo))
{
//$('#orderWymiar').attr('value', dlugosc);
$('#sila').css('border', '1px solid green');
$('.silaInfo').text('Statement true');
}else{
$('#wymiar').css('border', '1px solid red');
$('.silaInfo').text('silaOd must be more than' + silaOd + ', and less than ' + silaDo);
}
});

And, the wird thing is that, when force is 20, JavaScript says that it passed the statemenst, when in input .silaOd is typed 100. (silaOd meand minForce, and sildaDo means maxForce in Poland)

I dont have any idea, why that is happening. Please help! :)

There's a working example: http://jsfiddle.net/rJA6U/

1
  • I hope those variables are understandable in a language you speak, because if not that's just confusing... Commented Mar 31, 2011 at 14:09

3 Answers 3

3

You need to run parseInt() on the input values, like so:

var dlugosc = parseInt($('#sila').val());
var silaOd = parseInt($('.silaOd').val());
var silaDo = parseInt($('.silaDo').val());
Sign up to request clarification or add additional context in comments.

1 Comment

you might consider this as well, stackoverflow.com/questions/2240490/….
1

You are comparing strings so < or > does not work. You need to cast them as int.

example:

parseInt(dlugosc) > parseInt(sila0d)

Comments

1

try this:

$.fn.tryParseInt = function(defaultValue) {

    var 
        retValue = defaultValue,
        actualValue = $(this).val();

    if (actualValue != null) {
        if (actualValue.length > 0) {
            if (!isNaN(actualValue)) {
                retValue = parseInt(actualValue);
            }
        }
    }

    return retValue;
}


var 
  dlugosc = $('#sila').tryParseInt(0),
  silaOd = $('.silaOd').tryParseInt(0),
  silaDo = $('.silaDo').tryParseInt(0);

based on this link

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.