Как проверить пароль в HTML - PullRequest
0 голосов
/ 28 октября 2019

Вот мой HTML-код для страницы регистрации, но я не могу проверить поле пароля. что происходит здесь, всякий раз, когда я ввожу пароль и нажимаю на кнопку «Подтвердить пароль», отображается сообщение об ошибке, даже не позволяя мне снова вводить пароль. Пожалуйста, помогите мне исправить это! спасибо!

<!DOCTYPE html>
<html>
<head>
    <title>Sign up form</title>
    <link rel="stylesheet" type="text/css" href="signup.css">
</head>
<body>
    <div class="signup">

            <form action="insert.php" method="POST" onclick="return validation();">
                  <div id="eresult" style="color: red;"></div>
        <p>Username</p>
            <input type="text" name="username" placeholder="Enter New Username" required id="name">
            <p>Email</p>
            <input type="Email" name="email" placeholder="Enter Email" required id="email">
            <p>Password</p>
            <input type="password" name="pass" placeholder="Enter Password" required id="pass">
            <p>Confirm Password</p>
            <input type="password" name="confpass" placeholder="Confirm Password" required id="confpass">
            <input type="submit" name="" value="Submit">

        </form>
    </div>

      <script type="text/javascript">
      function validation(){
            var name = document.getElementById('name').value;
            var email= document.getElementById('email').value;
            var password = document.getElementById('pass').value;
            var confpassword = document.getElementById('confpass').value;

            if(password == confpassword){
                  return true;

            }
            else{
                  alert("Password does not match!!");

                  return false;
            }
      }

      </script>
</body>
</html>

Ответы [ 2 ]

1 голос
/ 28 октября 2019

Ошибка в том, что в html-элементе формы вы вызываете событие onclick, которое выполняется, когда вы щелкаете по любому html-элементу, поэтому передаваемое значение и значение confpass не определены, пока вы не измените один из них, поэтому вы видите предупреждение. Решение: использовать событие onsubmit.

<form action="insert.php" method="POST" onsubmit="return validation();">
0 голосов
/ 28 октября 2019

Вы можете обновить HTML с сервера или из кэша с помощью location.reload(true)

<!DOCTYPE html>
<html>
<head>
    <title>Sign up form</title>
    <link rel="stylesheet" type="text/css" href="signup.css">
</head>
<body>
    <div class="signup">

            <form action="insert.php" method="POST" onclick="return validation();">
                  <div id="eresult" style="color: red;"></div>
        <p>Username</p>
            <input type="text" name="username" placeholder="Enter New Username" required id="name">
            <p>Email</p>
            <input type="Email" name="email" placeholder="Enter Email" required id="email">
            <p>Password</p>
            <input type="password" name="pass" placeholder="Enter Password" required id="pass">
            <p>Confirm Password</p>
            <input type="password" name="confpass" placeholder="Confirm Password" required id="confpass">
            <input type="submit" name="" value="Submit">

        </form>
    </div>

      <script type="text/javascript">
      function validation(){
            var name = document.getElementById('name').value;
            var email= document.getElementById('email').value;
            var password = document.getElementById('pass').value;
            var confpassword = document.getElementById('confpass').value;

            if(password == confpassword){
                  return true;

            }
            else{
                  alert("Password does not match!!");
                 location.reload(true)
                  return false;
            }
      }

      </script>
</body>
</html>
...