Pavel Fára
Pavel Fára

Reputation: 5

Else is not executing ever

Im trying to do a form, in which you put your first name, surname and city, if inputs are empty or have number in them it should say Please fill out all of available boxes and make sure there are no numbers. Else it should say quote using all of input informations. But the else is not working.

I tried cahnging the code and swapping some variables.

function FillInfo()
{
         /* proměnné */
         var jmeno = document.forms ["SignUpForm"] ["jmeno"].value;
         var prijmeni = document.forms ["SignUpForm"] ["prijmeni"].value;
         var rok = document.forms ["SignUpForm"] ["mesto"].value;
         /*Kontrola zdali input políčka jsou prázdná či pokud bylo zadáno číslo */
         if(jmeno=="" || jmeno!=NaN || prijmeni=="" ||  prijmeni!= NaN || mesto=="" || mesto!=NaN){  

            document.getElementById("info").innerHTML = "Please fill out all of available boxes and make sure there are no numbers";    
      }
         else{     

            document.getElementById("info").innerHTML =  "Thank you" + " " + jmeno + " " + prijmeni + " from" + " " + mesto + "." + " " + "You are now being considered as our next adventurer. Good luck!";
    }
 }
<div class="heading2">
        <div class="container2">
            <p>Do you want to travel troughout space? Then fill out our form!</p><br>
            <form name="SignUpForm">
                <input type="text" name="jmeno" placeholder="First name" required><br>
                <input type="text" name="prijmeni" placeholder="Last name" required><br>
                <input type="text" name="mesto" placeholder="City" required><br><br>
                <div id="info" class="well"></div>
                <input type="button" class="otherpage" onclick="FillInfo();" value="Submit" /><br><br>
                <a href="Mainpage.html" class="BeginLink">Return</a>
            </form>  
       </div>      
    </div>

Upvotes: 0

Views: 93

Answers (2)

pgiammel
pgiammel

Reputation: 61

Your if condition has to change, it always evaluates to true.

Instead of:

if (jmeno=="" || jmeno!=NaN || prijmeni=="" ||  prijmeni!= NaN || mesto=="" || mesto!=NaN) {

You should try:

if (jmeno==="" || isNaN(jmeno) || prijmeni==="" || isNaN(prijmeni) || mesto==="" || isNaN(mesto)) {

By the way, NaN is never equal to NaN, you have to use isNaN to know if it's a NaN.

However, this code is not what actually want. You want to check that there are no numbers, right? Depending on if you want no digits at all or no number-only values, you have to adapt your code. For example: !isNaN(Number(jmeno)) to check if the value is a number-only value. The values you get from the text inputs are always strings so the conversion is needed.

Upvotes: 2

supertux
supertux

Reputation: 2189

Your logic is wrong

jmeno=="" || jmeno!=NaN

Will always evaluate to true, I think you mean

jmeno=="" || isNaN(jmeno)

Obviously the rest of the statement needs editing too.

Upvotes: 0

Related Questions