Thursday, 15 April 2010

Comparing 2 javascript numeric values in IF condition -


I have set up a validation function in JavaScript to validate values ​​from the HTML page:

  & lt; Script & gt; Validate the function () {var tterr = document.sngo2a.ttime; // This variable area captures the value in ttime var ttserr = document.sngo2a.sngottime; // This variable field captures the value in sngottime var errortimecheck = 0; If (ttserr.value & gt; tterr.value) {errortimecheck = 1; Var Sentence 31 = "Error !! \ n \ nTake time to stop in time- and go \ n can not be more than the time of full journey"; Warning (sentence31); Warning (ttserr.value); Warning (tterr.value); } And {errortimecheck = 0; }} & Lt; / Script & gt;  

I get the following values ​​from the html page:

ttime = 10 / sngottime = 7

then I expected not to see any warning message. However, I get the alert message "error ..... time in travel ........."

To confuse things even further, when I was 7 to 1 Until changed to sngottime Logic runs fine.

When values ​​are displayed for tterr.value and ttserr.value, they start displaying correctly.

Can anyone help me find out the problem?

I am making a assumption here that your ttime and

This means that JavaScript will evaluate them in the lexographic order (alphabetical order).

Therefore, in alphabetical order, your evaluation will check:

  1. Does 7 appear alphabetically in the form of 10
  2. It goes to the first block of your code and warnings
  3. <

    1. Does 1 number appear in alphabetical order after 10?
    2. Do not! Go to the other part of your statement

    To resolve this, explicitly enter the integers (or any other numerical type) your values:

      if (parseInt (ttserr.value)> parseInt (tterr.value)) {errortimecheck = 1; Var Sentence 31 = "Error !! \ n \ nTake time to stop in time- and go \ n can not be more than the time of full journey"; Warning (sentence31); Warning (ttserr.value); Warning (tterr.value); } And {errortimecheck = 0; }  

No comments:

Post a Comment