Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner at coding and I just recently tried out making a simple login page with an external javascript linked to it ... But when I start up the HTML and click the show button the javascript won't function.

The HTML CODE:

<HTML>

  <HEAD>
    <TITLE> Log In | Page </TITLE>
      <SCRIPT Type='text/javascript' src="main.js">
      </SCRIPT>
  </HEAD>

  <BODY>
    <CENTER>
        <FONT Face="Lucida Bright" size="28">  WELCOME 
        </FONT>
            <BR> <BR>
      <FORM Name="Mainform">
        <FONT Face="Lucida Bright" size="5">
          Username    
          <INPUT Type="text" Name="Usrnm" size="15">
            <BR> <BR>
          Password    
          <INPUT Type="password" Name="Usrps" size="15">
            <BR> <BR> <BR>
          <INPUT Type="button" Name="Login" Value="Show" onclick="event()">
        </FONT>
      </FORM>
    </CENTER>
  </BODY>

</HTML>


The JAVASCRIPT CODE:

function event() {
  var usrread= document.Mainform.Usrnm.value;
  var pswread= document.Mainform.Usrps.value;

    if (usrread.length<=3)
      {
        alert("Your username must contain at least 4 Characters!");
          alert("Wrong Data! Please check again.");
        return;
      }

    if (pswread.lenth<=5)
    {
      alert("Your password must contain at least 6 Characters!");
        alert("Wrong Data! Please check again.");
      return;
    }

    if (isNaN(pswread.charAt(0))==True || isNaN(pswread.charAt(1))==True)
    {
      alert("The first two characters in the Password must be Numbers!");
        alert("Wrong Data! Please check again.");
      return;
    }

    if (usrread.length>=3 && pswread.lenth>=5 && isNaN(pswread.charAt(0))==False
        && isNaN(pswread.charAt(1))==False)
    {
      alert("Correct Data");
      return;
    }
}



Please Help! Thank you in advance!

What I have tried:

I tried the online validators, and it says there are no errors. So I guess there must be some logic error. I would be so happy if you would point that out. :)
Posted
Updated 8-Sep-18 20:03pm
v2
Comments
Richard Deeming 11-Sep-18 14:06pm    
It looks like you're learning from a very old source. You're missing the doctype declaration; you're using deprecated elements like CENTER and FONT; and the element and attribute names should really be in lower-case.

(The case isn't technically required, but you'll want to be consistent, and the standard convention is to use lower-case.)
<!DOCTYPE html>
<html>
<head>
    <title>Log In | Page</title>
    <script src="main.js"></script>
	
	<style>
	body {
		font-family: "Lucida Bright";
		text-align: center;
	}
	</style>
</head>
<body>
    <h1>Welcome</h1>
    <form name="mainform">
        <p>username <input type="text" name="usrnm" size="15" pattern=".{4,}"></p>
        <p>password <input type="password" name="usrps" size="15" pattern="\d{2}.{4,}"></p>
        <p><input type="button" name="login" value="show" onclick="showDetails()"></p>
    </form>
</body>
</html>

function showDetails() {
    var usrread= document.mainform.usrnm.value;
    var pswread= document.mainform.usrps.value;
    
    if(usrread.length <= 3) {
        alert("Your username must contain at least 4 Characters!");
        return;
    }
    if (pswread.lenth <= 5) {
        alert("Your password must contain at least 6 Characters!");
        return;
    }
    if (!(/^\d{2}/).test(pswread)) {
        alert("The first two characters in the Password must be Numbers!");
        return;
    }
    
    // If we get here, all of the tests have passed; 
    // there's no need to repeat them.
    alert("Correct Data");
}


HTML input pattern Attribute[^]
Regular Expressions - JavaScript[^]

1 solution

Change the function name: JavaScript Reserved Words[^]
 
Share this answer
 
Comments
Patrice T 9-Sep-18 2:39am    
update your question with your code changes.
Member 13977771 9-Sep-18 2:40am    
@ Patrice T I got it to work somehow... Thank you for so much for willing to help!
Peter Leow 9-Sep-18 2:48am    
There are some more errors, the hints are:
1. typo
2. case sensitivity - https://www.w3schools.com/js/js_booleans.asp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900