Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having four text boxes in my application I want to check whether the total of the four text boxes should be equal to hundred. If it is less than and greater than hundred I want to through the error to the customer. It should be done in the keyUp or text change event using javascript. How to do this .

Thanks in Advance
Posted

1 solution

add class="calculate" to your 4 textboxes
JavaScript
$('.calculate').on('keyup', function(){
  var sum = 0;
  $('.calculate').each(function(){
    sum += parseFloat($(this).val());
  });
  if(sum>100){
    alert('Sum is greater than 100')
  }
  if(sum<100){
    alert('Sum is less than 100')
  }
});


This solution requires jQuery. If you don't use it yet, do something about it. Add this script ref in your document:
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 
Share this answer
 
v3
Comments
Espen Harlinn 19-Jul-12 9:21am    
Excellent reply :-D
StianSandberg 19-Jul-12 10:15am    
Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900