-1

Hello guys I'm really new to javascript,
I want an alert message to appear if the user pressed on check,
to see if the input was right or wrong,
and I've been trying for so long but it's not working no matter what, it's just a simple code.

I'm really suffering with this for hours, trying to figure why no alert message appears when I press on the check button.

Would be really grateful for any help or any tip, many thanks!!

function myFunction() {
  var x = document.getElementById("f").value;
  var z = document.getElementById("s").value;
  var c = document.getElementById("e").value;
  var v = document.getElementById("p").value;
  var b = document.getElementById("p1").value;

  if (isNaN(x) || x = "") {
    alert("First Name input is wrong");
  } else {
    alert("Input is ok");
  }


  if (isNaN(z) || z = "") {
    alert("Second Name input is wrong");
  } else {
    alert("Input is ok");
  }

  if (isNaN(c) || c = "") {
    alert("Email input is wrong");
  } else {
    alert("Input is ok");
  }

  if (v = "") {
    alert("Input is wrong");
  } else {
    alert("Input is ok");
  }

  if (b = "" || b !== v) {
    alert("Password does not match");
    else {
      alert("Input is ok");
    }
  }
}
<div class="event">
  <h1> Event Planning</h1>
</div>
<div class="back">

  <table border="1">
    <form action="submit.php" method="post">
      <tr>
        <th colspan="2"> Sign UP</th>
      </tr>
      <tr>
        <td> First Name </td>
        <td> <input type="text" name="FirstName" id="f"></td>
      </tr>
      <tr>
        <td> Second Name</td>
        <td> <input type="text" name="SecondName" id="s"></td>
      </tr>
      <tr>
        <td> Email Address</td>
        <td> <input type="email" name="email" id="e"> </td>
      </tr>
      <tr>
        <td> Password</td>
        <td> <input type="password" name="psd" id="p"> </td>
      </tr>
      <tr>
        <td> Repeat Password</td>
        <td> <input type="password" name="psd2" id="p1"> </td>
      </tr>
      <tr>
        <td colspan="2"> <input type="submit" name="submit" value="submit">
          <button type="button" id="but" onclick="myFunction()">Check</button></td>
      </tr>
    </form>
  </table>
Mister Jojo
  • 12,060
  • 3
  • 14
  • 33

1 Answers1

0

You have a few syntax errors in your function.

Javascript requires == (or ===) for comparisons.

You also need to add a curly brace } before the else after "Password does not match", and remove one after the following "Input is ok".

  • Additional note for OP, you could have easily find out about this issue by using tool such as Chrome or Firefox Developer Console. Recommend checking that out. it will send you a lots of time. Also you might consider getting better IDE - a modern IDE will tell you about mistakes you've made in realtime. – Patrik Stas Jan 14 '20 at 22:07