NatJohns NatJohns - 27 days ago 13
Javascript Question

Password Validation javascript

i am trying to create a very very basic profile page using Name, Email, Username, and Password. I have to have a password validation code/button.

The home page will be very similar to a common profile page. The user must be able to input the following.


  • Name field

  • Email field

  • User ID field

  • Password field 3

  • Validation Password field

  • The following buttons are required:

  • Password validation button

  • Create Profile button



I can put it all together, but the problem I am having is that the javascript console is telling me that there are some errors in the code...

HTML:

<head>

<script type = "text/javascript" src = "Profile Page.js"></script>

</head>

<body>

Enter First and Last Name<input type = "text" id = "name"><br>

Enter Your Email Address<input type = "text" id = "email"><br>

Please Enter a Username<input type = "text" id = "username"><br>

Please Enter a Password<input type = "password" id = "password"><br>

Enter Your Password Again<input type = "Password" id = "password2"><br>

<button type = "button" id = "validate" onClick = "validate()">Validate Password</button>

<button type = "button" id = "create" onClick = "submit()">Create Profile</button>


</body>


Javascript:

function validate(){


var pass1 = document.getElementById('password');

var pass2 = document.getElementById('Password2');

if (pass1 == pass2)

{
alert(Passwords Match)

}

else

{
alert(Passwords Do Not Match)
}}


ok, so i figured out where my errors were, now the alert that i set up for the passwords not matching is coming up, even when the passwords are the same thing. any suggestions?

Answer

The syntax errors are from:

var pass1 = eval(document.getElementById('password');
var pass2 = eval(document.getElementById('password2');
if pass1 == pass2

You need to change it to:

var pass1 = document.getElementById('password').value;
var pass2 = document.getElementById('Password2').value;
if (pass1 == pass2)

So the whole code will look like this.

function validate(){
    var pass1 = document.getElementById('password').value;
    var pass2 = document.getElementById('Password2').value;

    if (pass1 == pass2)
    {
        alert("Passwords Match")
    }

    else
    {
        alert("Passwords Do Not Match")
    }
}
<head>
    <script type="text/javascript" src="Profile Page.js"></script>
</head>

<body>
    Enter First and Last Name
    <input type="text" id="name">
    <br>Enter Your Email Address
    <input type="text" id="email">
    <br>Please Enter a Username
    <input type="text" id="username">
    <br>Please Enter a Password
    <input type="password" id="password">
    <br>Enter Your Password Again
    <input type="Password" id="password2">
    <br>

    <button type="button" id="validate" onClick="validate()">Validate Password</button>
    <button type="button" id="create" onClick="submit()">Create Profile</button>
</body>

What are the rules for your password validation?

Comments