-1

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:

function validate() {
  let regName = /^[a-zA-Z]+ [a-zA-Z]+$/;
  let name = document.querySelector("[name='user-name']");
  let msg = document.querySelector("[name='user-message']");
  
  if (name) {
      name = name.value;   
  } else if (!regName.test(name)){
    document.querySelector("[name='user-name']");
    return "Please enter your first & last name.";    
  }

  if (msg) {
      msg = msg.value;
  } else if (msg.trim() == "") {     
    document.querySelector("[name='user-message']");
    return "Please enter a message";
  } 
} 
7
  • Isn't any person's name "valid" no matter how its spelled? I could go to court tomorrow and change my name to c++ if I wanted to. ... So do you need to check on validity of someone's "name" other it not being an empty string, stripped of all white space? Commented Apr 16, 2022 at 4:33
  • well, im trying to check for a first and lastg name, yes you could legally do that but if you had to enter your info into a form how would that work? Commented Apr 16, 2022 at 4:35
  • You're only doing regName.test(name) if the user-name input can't be found. Why do you have that code in else if? Commented Apr 16, 2022 at 4:36
  • The form will accept whatever name I entered. Ideally. But if you want non-outlier cases, you can always have a "lookup" table of common last names. The first name is all bets off. Commented Apr 16, 2022 at 4:37
  • I didnt know how to do both checks together Commented Apr 16, 2022 at 4:37

1 Answer 1

1

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.

function validate() {
  let regName = /^[a-zA-Z]+ [a-zA-Z]+$/;
  let name = document.querySelector("[name='user-name']");
  let msg = document.querySelector("[name='user-message']");

  if (name && !regName.test(name.value)) {
    return "Please enter your first & last name.";
  }

  if (msg && msg.value.trim() == "") {
    return "Please enter a message";
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

Ok thank you. I had tried this already but I didnt have the .value attached to the second condition, That is why I broke it down to separate if statements. Thanks for clearing that up.
You could also do it with separate variables, but those lines should be inside the if block, right after you assign name = name.value;. Not in else if. Just think about what the word "else" means in English.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.