2017-12-15 39 views
0

機能を追加する方法にいくつかの問題があります。誰かが私に「名前/パスワードフィールドが空である」というポップアップメッセージを表示する機能を追加する方法を教えてもらえますか? ログインフィールドを空にしてログインすると、ここに問題があります。彼らは空だったのに?私は任意のPHP関数を使用することはできません。ユーザーが何かを入力したかどうかをチェックする機能

<html> 
<head> 
<title>Week 9 Q&A Session - Form Demo</title> 
    </head> 
    <body> 

<!--Registration form --> 
<div> 
    <h1>Registration</h1> 
    <form onsubmit="registerUser()"> 
<input type="text" name="username" value="" id="name" required> 
<input type="password" name="password" id="password" required> 
<button type="submit">Submit</button> 
</form> 
</div> 

<!-- Login form --> 
<div> 
    <h1>Login</h1> 
    <input type="text" name="username" value="" id="loginName"> 
    <input type="password" name="password" id="loginPassword"> 
    <button onclick="checkLogin()">Submit</button> 
    <p id="LoginResult">Not logged in.</p> 
</div> 

<!-- Rankings table will be inserted here --> 
<div id="RankingsTable"></div> 

<script> 

    /* Does some basic checking of user data then stores 
     user data in localStorage */ 
    function registerUser(){ 
     //Extract the name and password that the user has entered 
     var nameInput = document.getElementById("name").value; 
     var pwdInput = document.getElementById("password").value; 

     //Check that the name and password are not empty 
     if(nameInput !== "" && pwdInput !== ""){ 
      //Create a JavaScript object to hold the user data. 
      var usrObj = {}; 

      //Add user entered data to object 
      usrObj.username = nameInput; 
      usrObj.password = pwdInput; 

      //Add a score field to object to support rankings table 
      usrObj.topscore = 0; 

      //Store a string version of the object in local storage. 
      localStorage[nameInput] = JSON.stringify(usrObj); 
     } 
    } 


    /* Checks that the username and password match the user name and password of a 
     registered user and provides feedback to user. */ 
    function checkLogin(){ 
     //Get a reference to the div where we will display the login result 
     var loginResult = document.getElementById("LoginResult"); 

     //Extract the name and password that the user has entered 
     var nameInput = document.getElementById("loginName").value; 
     var pwdInput = document.getElementById("loginPassword").value; 

     //Output for debugging 
     console.log("Login name: " + nameInput+ "; Login password" + pwdInput); 

     //Check to see if we have data stored for this user 
     if(localStorage[nameInput] === undefined){ 
      //No user found - provide feedback to user. 
      loginResult.innerHTML = "User name incorrect"; 
      return; 
     } 

     //Check password 
     //Get object that is stored for the user name. 
     var usrObj = JSON.parse(localStorage[nameInput]); 

     //Compare the entered password with the stored password 
     if(pwdInput !== usrObj.password){ 
      //Incorrect password - provide feedback to user 
      loginResult.innerHTML = "Password incorrect"; 
      return; 
     } 

     //If we have got this far, the username and password are correct 

     //Record the user that has logged in using local storage. 
     localStorage.loggedInUser = nameInput; 

     //Provide feedback to user - you could also provide a logout button - see the example in my slides. 
     loginResult.innerHTML = "User logged in."; 
    } 


    /* This function is called when a logged in user 
     plays the game and gets a score */ 
    function updateScore(newScore){ 
     //Get the JavaScript object that holds the data for the logged in user 
     var usrObj = JSON.parse(localStorage[localStorage.loggedInUser]); 

     //Update the user object with the new top score 
     /* NOTE YOU NEED TO CHANGE THIS CODE TO CHECK TO SEE IF THE NEW SCORE 
      IS GREATER THAN THE OLD SCORE */ 
     usrObj.topscore = newScore; 

     //Put the user data back into local storage. 
     localStorage[localStorage.loggedInUser] = JSON.stringify(usrObj); 
    } 


    /* Loads the rankings table. 
     This function should be called when the page containing the rankings table loads */ 
    function showRankingsTable(){ 
     //Get a reference to the div that will hold the rankings table. 
     var rankingDiv = document.getElementById("RankingsTable"); 

     //Create a variable that will hold the HTML for the rankings table 
     var htmlStr = ""; 

     //Add a heading 
     htmlStr += "<h1>Rankings Table</h1>"; 

     //Add the table tag 
     htmlStr += "<table>"; 

     //Work through all of the keys in local storage 
     for(var key in localStorage) { 
      //All of the keys should point to user data except loggedInUser 
      if(key !== "loggedInUser"){ 
       //Extract object containing user data 

       //Extract user name and top score 
       htmlStr += "David"; 
       //Add a table row to the HTML string. 
      } 
     } 

     //Finish off the table 
     htmlStr += "</table>"; 

     //Add the table to the page. 

    } 

</script> 

+0

あなたは赤い色で提出されたそれぞれのテキストの下にエラーメッセージを表示することができます。 –

+0

入力タグ –

+0

で「必須」のようなhtml5検証を使用することもできますが、試しましたが動作しません。あなたがコードを試して、それがあなたのために働くかどうか教えてください。 –

答えて

0

必要な入力フィールドを作成するには、

  • をすべきことは、あなたの<button>は、 "送信" タイプ
  • で設定し、実際の<form>
  • であなたのフォームフィールドをラップボタンではなく、フォームonsubmitでイベントをトリガーする10イベント
  • そして、単にフォームを最初にチェックがあれば、これが(真の送信ボタンにより)フォームが送信されたときにために動作

にHTML要素に
<form onsubmit="registerUser()"> 
    <input type="text" name="username" value="" id="name" required> 
    <input type="password" name="password" id="password" required> 
    <button type="submit">Submit</button> 
</form> 

required属性を設定しますすべてのフィールドバリデーターが満足されています。 requiredフィールドが入力されていない場合、フォームは送信されません。したがって、フォームが完全に有効な場合にのみ、registerUser()関数が呼び出されます。submitイベントが発生するためです。

+0

私のコードにそれを適用できますか?私はそれを試したが、それは私のために働いていない –

+0

@ NatalieMcKnight私はあなたのコードに基づいて私の答えでそれを示すコードの例があります。 – Soviut

+0

私のコードを更新しました。まだそれは私のために働いていません:/ –

0

この行をコードに追加するだけです。 checkLogin()機能。フィールドのいずれかが空であるかどうかをチェックすることです。空の場合は、テキストを「フィールドを入力してください」に変更します。また、ほとんどのブラウザがあなたのためにそれを行うにもかかわらず、</ body>タグで終わらないことも良い習慣です。ここで

if(nameInput.length === 0 || pwdInput.length == 0) 
 
{ 
 
    loginResult.innerHTML = "Please fill the fields."; 
 
    return; 
 
}

私はちょうどあなたの既存のコードにその部分を追加しました。代わりに、ポップアップの

<html> 
 
<head> 
 
    <title>Week 9 Q&A Session - Form Demo</title> 
 
</head> 
 

 
<body> 
 

 
    <!--Registration form --> 
 
    <div> 
 
     <h1>Registration</h1> 
 
     <form onsubmit="registerUser()"> 
 
     <input type="text" name="username" value="" id="name" required> 
 
     <input type="password" name="password" id="password" required> 
 
     <button type="submit">Submit</button> 
 
     </form> 
 
    </div> 
 

 
    <!-- Login form --> 
 
    <div> 
 
     <h1>Login</h1> 
 
     <input type="text" name="username" value="" id="loginName"> 
 
     <input type="password" name="password" id="loginPassword"> 
 
     <button onclick="checkLogin()">Submit</button> 
 
     <p id="LoginResult">Not logged in.</p> 
 
    </div> 
 

 
    <!-- Rankings table will be inserted here --> 
 
    <div id="RankingsTable"></div> 
 

 
    <script> 
 

 
     /* Does some basic checking of user data then stores 
 
     user data in localStorage */ 
 
     function registerUser(){ 
 
      //Extract the name and password that the user has entered 
 
      var nameInput = document.getElementById("name").value; 
 
      var pwdInput = document.getElementById("password").value; 
 

 
      //Check that the name and password are not empty 
 
      if(nameInput !== "" && pwdInput !== ""){ 
 
       //Create a JavaScript object to hold the user data. 
 
       var usrObj = {}; 
 

 
       //Add user entered data to object 
 
       usrObj.username = nameInput; 
 
       usrObj.password = pwdInput; 
 

 
       //Add a score field to object to support rankings table 
 
       usrObj.topscore = 0; 
 

 
       //Store a string version of the object in local storage. 
 
       localStorage[nameInput] = JSON.stringify(usrObj); 
 
      } 
 
     } 
 

 

 
     /* Checks that the username and password match the user name and password of a 
 
     registered user and provides feedback to user. */ 
 
     function checkLogin(){ 
 
      //Get a reference to the div where we will display the login result 
 
      var loginResult = document.getElementById("LoginResult"); 
 

 
      //Extract the name and password that the user has entered 
 
      var nameInput = document.getElementById("loginName").value; 
 
      var pwdInput = document.getElementById("loginPassword").value; 
 

 
      //Output for debugging 
 
      console.log("Login name: " + nameInput+ "; Login password" + pwdInput); 
 
      if(nameInput.length === 0 || pwdInput.length == 0) 
 
      { 
 
       loginResult.innerHTML = "Please fill the fields."; 
 
       return; 
 
      } 
 
      //Check to see if we have data stored for this user 
 
      if(localStorage[nameInput] === undefined){ 
 
       //No user found - provide feedback to user. 
 
       loginResult.innerHTML = "User name incorrect"; 
 
       return; 
 
      } 
 

 
      //Check password 
 
      //Get object that is stored for the user name. 
 
      var usrObj = JSON.parse(localStorage[nameInput]); 
 

 
      //Compare the entered password with the stored password 
 
      if(pwdInput !== usrObj.password){ 
 
      //Incorrect password - provide feedback to user 
 
      loginResult.innerHTML = "Password incorrect"; 
 
      return; 
 
     } 
 

 
     //If we have got this far, the username and password are correct 
 

 
     //Record the user that has logged in using local storage. 
 
     localStorage.loggedInUser = nameInput; 
 

 
     //Provide feedback to user - you could also provide a logout button - see the example in my slides. 
 
     loginResult.innerHTML = "User logged in."; 
 
     } 
 

 

 
     /* This function is called when a logged in user 
 
     plays the game and gets a score */ 
 
     function updateScore(newScore){ 
 
      //Get the JavaScript object that holds the data for the logged in user 
 
      var usrObj = JSON.parse(localStorage[localStorage.loggedInUser]); 
 

 
      //Update the user object with the new top score 
 
      /* NOTE YOU NEED TO CHANGE THIS CODE TO CHECK TO SEE IF THE NEW SCORE 
 
      IS GREATER THAN THE OLD SCORE */ 
 
      usrObj.topscore = newScore; 
 

 
      //Put the user data back into local storage. 
 
      localStorage[localStorage.loggedInUser] = JSON.stringify(usrObj); 
 
     } 
 

 

 
     /* Loads the rankings table. 
 
     This function should be called when the page containing the rankings table loads */ 
 
     function showRankingsTable(){ 
 
      //Get a reference to the div that will hold the rankings table. 
 
      var rankingDiv = document.getElementById("RankingsTable"); 
 

 
      //Create a variable that will hold the HTML for the rankings table 
 
      var htmlStr = ""; 
 

 
      //Add a heading 
 
      htmlStr += "<h1>Rankings Table</h1>"; 
 

 
      //Add the table tag 
 
      htmlStr += "<table>"; 
 

 
      //Work through all of the keys in local storage 
 
      for(var key in localStorage) { 
 
       //All of the keys should point to user data except loggedInUser 
 
       if(key !== "loggedInUser"){ 
 
       //Extract object containing user data 
 

 
       //Extract user name and top score 
 
       htmlStr += "David"; 
 
       //Add a table row to the HTML string. 
 
       } 
 
      } 
 

 
      //Finish off the table 
 
      htmlStr += "</table>"; 
 

 
      //Add the table to the page. 
 

 
     } 
 

 
    </script> 
 
    
 
</body>

関連する問題