0

Hi below is a section of my code. I dont get the desired out put for else if ( txt3Val.value >= txt4Val ) but if I adjust it to else if ( document.getElementById('txt3') >= document.getElementById('txt4') ) it works. Can someone tell me why this happens? Thanks.

......AND THE CODE READS
    else if(document.form1.GWchk.checked == true) 
        {
        var txt3Val = document.getElementById('txt3');
        var txt4Val = document.getElementById('txt4');
            if(txt3Val.value == "" || txt4Val.value == "")
            {
                alert ("You need to enter both starting and ending number \nof the cheque book for GlW."); return false;
            }
            else if ( txt3Val.value >= txt4Val ) 
            { 
                alert ("Ending cheque number should be of greater value"); return false;
            }
            else
            {
            alert ("Successfully saved your entry1."); return false;
            }
        }

HIGHLIGHT : Sorry the code else if ( txt3Val.value >= txt4Val.value ) also doesnt work! Actually that's what its there on my real script a re-write error here. But the my preoblem remains the same where it doesnt give me the ending number should be greate.... and straight goes to successfully saved.

EDIT I think I figure out something. On the first text box if I put '10' as a value and second as '9' then JS seems doesn't recognize 9 as a lower value. But if I put '09' it gives the desired out put. How do I handle this?

3
  • How are you going to compare two HTML elements? Use the corresponding member according to the nature, i.e. value for <input> Commented Oct 29, 2012 at 14:59
  • Did you mean txt4Val.value in that if statement? Commented Oct 29, 2012 at 14:59
  • Shouldnt it be "else if ( txt3Val.value >= txt4Val.value )" instead of "else if ( txt3Val.value >= txt4Val )"? Commented Oct 29, 2012 at 15:00

4 Answers 4

3

try changing:

else if ( txt3Val.value >= txt4Val ) 

to

else if ( txt3Val.value >= txt4Val.value ) 
Sign up to request clarification or add additional context in comments.

Comments

2
else if ( txt3Val.value >= txt4Val )

should be

else if ( txt3Val.value >= txt4Val.value )

Comments

0
if( parseInt( txt3Val.value, 10 ) >=  parseInt( txt4Val.value, 10 ) )

Comments

0

Using comparisons again HTML elements isn't going to give you your desired result. Append .value to the end of txt4Val to access the value from that element:

else if ( txt3Val.value >= txt4Val.value ) {

}

Comments

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.