If / else else if in jQuery for a condition

I have a set of text fields where I perform a check. Let's say I have a field called "places" that can take a value less than "99999". While reading, I should not have "99999" any thing less than this is normal. For this, I wrote below if and else if. please tell me that I am doing something wrong. I am confused in the morning whether it should be smaller or larger than

if ($("#seats").val() != '') { setflag = false; alert("Not a valid character") } else if($("#seats").val() < 99999) { alert("Not a valid Number"); } else { setflag = true; } 
+6
jquery jquery-selectors
source share
4 answers

Yum a lot of confused in the morning, should it be less or more than `

it may take a value less than "99999"

I think you yourself answered this ... But it is valid when it is less . Therefore, the following is not true:

 }elseif($("#seats").val() < 99999){ alert("Not a valid Number"); }else{ 

You say if it is less than 99999, then it is invalid . You want to do the opposite:

 }elseif($("#seats").val() >= 99999){ alert("Not a valid Number"); }else{ 

Also, since you have $("#seats") twice, jQuery should look for the DOM twice. You really have to store the value, or at least the DOM element in a variable. And some more of your code doesn't make much sense, so I'm going to make some assumptions and all together:

 var seats = $("#seats").val(); var error = null; if (seats == "") { error = "Number is required"; } else { var seatsNum = parseInt(seats); if (isNaN(seatsNum)) { error = "Not a valid number"; } else if (seatsNum >= 99999) { error = "Number must be less than 99999"; } } if (error != null) { alert(error); } else { alert("Valid number"); } // If you really need setflag: var setflag = error != null; 

Here's a working example: http://jsfiddle.net/LUY8q/

+10
source share

A few more things in addition to the existing answers. Look at this:

 var seatsValid = true; // cache the selector var seatsVal = $("#seats").val(); if(seatsVal!=''){ seatsValid = false; alert("Not a valid character") // convert seatsVal to an integer for comparison }else if(parseInt(seatsVal) < 99999){ seatsValid = false; alert("Not a valid Number"); } 

The setFlag variable name is very common if you use it only in combination with the number of seats you should rename (I named it seatValid). I also initialized this to true, which eliminates the need for a finale in your source code. Then I put the selector and call .val () in the variable. It is good practice to cache your selectors, so jQuery should not traverse the DOM more than necessary. Finally, when comparing the two values, you should try to make sure that they are of the same type, in this case seatVal is a string, so to compare it correctly with 99999, you should use parseInt () on it.

+1
source share

Change the less-than statement to a greater or equal operator:

 }elseif($("#seats").val() >= 99999){ 
0
source share

See this answer . val () compares a string, not a numeric value.

0
source share

All Articles