How To Create Login Form Using JavaScript - Part II

Related source code: How To Create Login Form Using JavaScript This is an alteration of the last tutorial in the title of How To Create Login Form Using JavaScript that the users will proceed to the next URL if they type their username and password precisely. The changes of this tutorial, the users has given a chance to type their username and password precisely. And, they're only 3 chances. But, you can edit after you download the source code if you want more chances to be given to the users. Example Username and Password Username: User_1 Password: Password_1 To change the username and password information. This is the script.
  1. var usernameArray = ["User_1", "User_2", "User_3", "User_4"];
  2. var passwordArray = ["Password_1", "Password_2", "Password_3", "Password_4"];

Directions:

Kindly insert this code below into the BODY tag of your Login Form.
  1. <script type = "text/javascript">
  2.  
  3. var count = 2;
  4. function tryToLogin() {
  5. var un = document.loginPage.userName.value;
  6. var pw = document.loginPage.passWord.value;
  7. var valid = false;
  8.  
  9. var usernameArray = ["User_1", "User_2", "User_3", "User_4"];
  10. var passwordArray = ["Password_1", "Password_2", "Password_3", "Password_4"];
  11.  
  12. for (var i=0; i <usernameArray.length; i++) {
  13. if ((un == usernameArray[i]) && (pw == passwordArray[i])) {
  14. valid = true;
  15. break;
  16. }
  17. }
  18.  
  19. if (valid) {
  20. alert ("Login Successfully");
  21. window.location = "next.html";
  22. return false;
  23. }
  24.  
  25. var t = " chances";
  26. if (count == 1) {t = " chance"}
  27.  
  28. if (count >= 1) {
  29. alert ("Invalid userName or passWord. You have " + count + t + " to correct your details.");
  30. document.loginPage.userName.value = "";
  31. document.loginPage.passWord.value = "";
  32. setTimeout("document.loginPage.userName.focus()", 25);
  33. setTimeout("document.loginPage.userName.select()", 25);
  34. count --;
  35. }
  36.  
  37. else {
  38. alert ("Wrong Detail! You have no more chances to be given!");
  39. document.loginPage.userName.value = "No more chances to be given.!";
  40. document.loginPage.passWord.value = "";
  41. document.loginPage.userName.disabled = true;
  42. document.loginPage.passWord.disabled = true;
  43. return false;
  44. }
  45. }
  46.  
  47. </script>
  1.  
  2. <form name="loginPage">
  3.  
  4. <table cellpadding="5" cellspacing="5">
  5. <tr>
  6. <td colspan="3">
  7. <h1 style="text-align:center;">
  8. Login
  9. </h1>
  10. </td>
  11. </tr>
  12. <tr>
  13. <td>
  14. <h2>Username</h2>
  15. </td>
  16. <td width="10">
  17. </td>
  18. <td>
  19. <input type="text" name="userName" autofocus="autofocus" style="font-size:18px; text-indent:5px;" placeholder="Enter your Username">
  20. </td>
  21. </tr>
  22. <tr>
  23. <td>
  24. <h2>Password</h2>
  25. </td>
  26. <td width="10">
  27. </td>
  28. <td>
  29. <input type="password" name="passWord" autofocus="autofocus" style="font-size:18px; text-indent:5px;" placeholder="Enter your Password">
  30. </td>
  31. </tr>
  32. <tr>
  33. <td colspan="3" style="text-align:center;">
  34. <br />
  35. <input type="button" value="Login" onclick= "tryToLogin()" name="Submit" style="margin-right:20px; border-radius:4px; background-color:skyblue; font-size:18px; cursor:pointer;">
  36. <input type="reset" value="Reset" style="font-size:18px; cursor:pointer; border-radius:4px; background-color:orange;">
  37. <br />
  38. <br />
  39. </td>
  40. </tr>
  41.  
  42. </form>
  43.  
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial. For more updates, don’t hesitate and feel free to visit our website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you.

Add new comment