简体   繁体   中英

Javascript checking for blanks in form

I'm Dutch please be kind on the grammar. I've looked online and i cannot seem to figure out how to check if a form was submitted when empty.

I will not be adding code since it keeps telling me its not formatted properly even though i do that automatically in VSC I think the question is self explanatory but still. If u got a question ask it. I really need help ASAP i got the talk tomorrow.

When i erase(wis) it also erases the dropdown Heads or Tails(HorT) Which means its blank. When someone then plays then CANNOT win. If they do not put in a bet they will win/lose NaN.

I have checks in for if they have put <=0 and if they have the actual balance. I just cannot seem to figure this part out.

Any help would be amazing. I've tried if(HorT == ""), (HorT.length === 0) And a few others i didn't even understand what i was typing. Im a beginner in coding and had some fun with this assignment last year but now i have to present it for an intership place.

It says i have to explain more but i dont know how to explain more.

Using

if (HorT == "") {your code}

Should have worked. I think what you could have done wrong was not add the else part of the if statement. If you just did the if statement and skipped the else part, the code would continue running, but would also run the code within the if statement if the string was empty.

Basically, what this code does is check if the string (the letters) in the variable HorT is empty, and does the code inside the { } . So, if I wrote

if (HorT == "") {
console.log('empty') // or whatever you would like to happen if the submitted form is empty
} else {
    // what do to if the submitted form is NOT empty
}

It would log in your console empty when the variable is empty, and if it was not (thus, the else part of the statement), it would execute the rest of your code.

Good luck with your internship!

Edit: talked to him about debugging on discord. the parsed variable actually was 1 (found by logging the variable), so the else if statement should be else if (HorT == 1) . We also talked about handling different errors such as when HorT is NaN, in which we discussed having else if (HorT != HorT) {error handling} as the handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM