0

I am trying to take this code and add one piece to it. I would like the #total to have a maximum number. I am not even sure how to approach this one. I have looked around the query documentation at adding a validation or something but it wasn't what I wanted. Any help would be greatly appreciated. http://jsfiddle.net/anderskitson/VVkzG/1/

JQUERY:

$('#the_input_id').keyup(function() {
    updateTotal();
});

$('#the_input_id1').keyup(function() {
    updateTotal();
});

var updateTotal = function() {
    var input1 = parseInt($('#the_input_id').val());
    var input2 = parseInt($('#the_input_id1').val());
    if (isNaN(input1) || isNaN(input2)) {
        $('#total').text('');
    } else {
        $('#total').text(input1 + input2);
    }
};​
2
  • Like this? jsfiddle.net/VVkzG/2 Commented Feb 19, 2012 at 6:43
  • Exactly could you place this as a answer so I could mark it as such. Commented Feb 19, 2012 at 22:06

1 Answer 1

2

jsfiddle.net/VVkzG/2

HTML

<form method="post">
    <input type="text" id="the_input_id">
    <input type="text" id="the_input_id1">
</form>

</div>
<div id="total">

</div>​

Javascript

$('#the_input_id').keyup(function() {
    updateTotal();
});

$('#the_input_id1').keyup(function() {
    updateTotal();
});

var updateTotal = function() {
    var input1 = parseInt($('#the_input_id').val());
    var input2 = parseInt($('#the_input_id1').val());
    if (isNaN(input1) || isNaN(input2)) {
        $('#total').text('');
    } else {
        var max = 500;
        var total = input1 + input2;

        if(total > max) {
            $('#total').text('The maximum is '+max);
        } else {
             $('#total').text(total);
        }


    }
};​
Sign up to request clarification or add additional context in comments.

1 Comment

Please post your code in your answer, some day jsfiddle might not be there.

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.