if statements in else block and vice versa

Viewed 35

User input is stored in variable "a". "rightUsr" is a regex condition to check a required input.

How do I make it as if “a[user input] “ does not match it should alert "something went wrong" in if block, and in else block it should print "welcome!"

   if(a.match(rightUsr) ){
      document.write("Welcome")
   }
   
   else{
     alert("something went wrong!")
   }
2 Answers

You have to use the logical NOT in JavaScript which is written as '!'. To implement your logic, the code is:

   if(!a.match(rightUsr) ){
      alert("something went wrong!")
   }
   
   else{
     document.write("Welcome")
   }
if(!a.match(rightUsr) ){
      alert("something went wrong!");
      return;
    }
   document.write("Welcome")
  
Related