Javascript compare two numbers

How to compare two Value in JavaScipts:

1.Get the Value From Html Page using getElementById

2.Compare by Using parseInt For Interger Value and parseFloat For Float Value

3.compare two value as

var first=document.getElementById('first').value;
var second=document.getElementById('second').value; 
 
if(parseInt(first)<parseInt(second))
alert('Second Value is Greater than first');
 
else if(parseInt(first)>parseInt(second))
alert('First Value is Greater than second');
 
else if(parseInt(first)==parseInt(second))
alert('First Value and second value is equal');

4.Include the code in Html Page For TextBox

First Value:<input type="text" value="0" onkeyup="compare();" name="first" id="first">
Second Value:<input type="text" value="0" onkeyup="compare();" name="second" id="second">

Demo Link

Popularity: 41% [?]

Related posts:

  1. JavaScript to Validate Email Format
  2. Compare two ArrayList in JSP
  3. set focus on textbox value at the end
  4. Set textbox background color when clicked
You can leave a response, or trackback from your own site.

4 Responses to “Javascript compare two numbers”

  1. Justin says:

    How to compare not equal to in java script?

  2. Rui says:

    How can I compare String value?

  3. Ravan says:

    Thank you very much… Instead of the above code..
    I changed the code below…..

    if(parseInt(first)< parseInt(second))
    {
    alert('Second Value is Greater than first');
    textfirst.focus();
    }

  4. Raf says:

    Im doing a similar thing but I want to have multiple comparisons but use the one javascript. at the moment the first set works perfectly but nothing after that. I could call the next set third and forth value etc but that would mean duplicating the javascript. is there a way around this?

Leave a Reply

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars