Skip to content
Advertisement

Validation issues in JavaScript

The following function is supposed to check two inputs: name and message. I’m not sure what is wrong here but I’m trying to first see if the input is empty if it’s not I want to then check it with a regular expression.

The message I just want to see if it is empty or not. If either are empty I want to return a message asking to enter the correct input. I’m not sure if I should use else ifs here or separate if statements altogether. If I’m doing this completely bonkers Id like to know that as well.

JavaScript:

JavaScript

Advertisement

Answer

Your if and else if logic is wrong. You’re only performing the regexp test when the input element can’t be found by document.querySelector(). else if only runs when the previous condition failed.

You should put that check in the same if that checks that the input was found.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement