Nikhil Solanki
Nikhil Solanki

Reputation: 31

If Statement issue or operator issue

Need help to find out what wrong has happened? Resulting output doesn't cover less then condition.

Operator in javascript not working or if else is not working.

function set_shield(t_value){

    var shield = document.getElementById('set_shield');
    if(shield.value==0){
        alert('Keep Enter a Value')
        exit;
    }
    else if(isNaN(shield.value)) {
        alert('Keep Enter Only numeric value')
        exit;
    }
    else if(t_value < shield.value) {
        alert('gf')
        exit;
    }
    else {
        var ok = confirm("Are you sure want Lock Value?")
        if (ok) {
            alert("I am an alert box!");
        }
        else {
            exit;
        }   
    }
}

Upvotes: 0

Views: 67

Answers (2)

Mackan
Mackan

Reputation: 6271

Force shield.value to int in every comparison, since you're using it as a numeric value.

parseInt(shield.value, 10)

example:

else if (parseInt(t_value, 10) < parseInt(shield.value, 10)) {

t_value is unkown for us, it might not need parseInt, but I assume it does.

Also exit is throwing a syntax error. Remove them all - not needed, does nothing.

Upvotes: 1

Jonathan Smith
Jonathan Smith

Reputation: 2599

Have a look at this, it works fine: http://jsfiddle.net/bd61xv1z/3/

HTML

<input id="set_shield" type="text" value="0" />

JAVASCRIPT

 function set_shield(t_value){

                var shield = document.getElementById('set_shield');
                    if(shield.value==0){
                            alert('Keep Enter a Value');
                            }

                    else if(isNaN(parseInt(shield.value, 10)))
                            {
                            alert('Keep Enter Only numeric value');
                                }
                    else if(t_value < shield.value){
                            alert('gf');
                            }   
                    else{   

                            var ok = confirm("Are you sure want Lock Value?")
                                    if (ok)
                                    {
                                         alert("I am an alert box!");


                                        }
                                    else{

                                        }   

                            }
    }

Upvotes: 0

Related Questions