Results 1 to 9 of 9

Thread: javascript password validator

  1. #1
    Senior Member
    Join Date
    May 2002
    Posts
    147

    javascript password validator

    I'm trying to write some javascript that will check that two passwords are equal when they are typed into a form. If not, a pop-up alert id displayed.
    However...it doesn't do anything. I'd be grateful for some help.

    <script type="text/javascript">

    function checkPass()

    {
    if (document.passwordForm.password.value)==(document.passwordForm.password2.value)
    {
    //load page
    }

    else
    {
    alert("Passwords don't match")
    }

    }


    </script>

    <form name="passwordForm">
    <p>Enter your name
    <input name="password" type="text" id="password">
    <input name="password2" type="text" id="password2">
    </p>
    <p>
    <input type="button" value=" Enter" onclick="checkPass()">
    </p>
    </form>
    Mama always said, keep your virus definitions up to date.

  2. #2
    Senior Member
    Join Date
    Jul 2003
    Posts
    634
    Im not to good at javascript, but like most programming lanaguges it looks like you need to declare the variable??

    var a = document.passwordForm.password.value
    var b = document.passwordForm.password2.value

    if (a == b) alert("correct");

    else
    {
    alert("Passwords don't match")
    }

    might work, I dont have time to try...

    i2c

  3. #3
    Senior Member
    Join Date
    Jul 2003
    Posts
    634
    Im not to good at javascript, but like most programming lanaguges it looks like you need to declare the variable??

    var a = document.passwordForm.password.value
    var b = document.passwordForm.password2.value

    if (a == b) alert("correct");

    else
    {
    alert("Passwords don't match")
    }

    might work, I dont have time to try...

    i2c

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    786
    I made one that worked. I think that your problems were from the weird <SCRIPT> tag. There must have been something else wrong because it still didn't work for me. Anyways, you can take my code and play with it to fit your page.

    BTW, just remember that right now, anyone who Views Source of this page can figure out what the next page is.

    Enjoy.


    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JAVASCRIPT">
    function checkPass()
    {
      if (document.PassForm.Password1.value == document.PassForm.Password2.value)
      {
        alert("Good!");
      }
      else
      {
        alert("Wrong!");
      }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="PassForm">
    Enter your password: <INPUT NAME="Password1"><BR>
    Enter your password (again): <INPUT NAME="Password2"><BR>
    <INPUT TYPE="BUTTON" VALUE="Enter" ONCLICK="checkPass()">
    </FORM>
    </BODY>
    </HTML>

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    786
    I made one that worked. I think that your problems were from the weird <SCRIPT> tag. There must have been something else wrong because it still didn't work for me. Anyways, you can take my code and play with it to fit your page.

    BTW, just remember that right now, anyone who Views Source of this page can figure out what the next page is.

    Enjoy.


    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JAVASCRIPT">
    function checkPass()
    {
      if (document.PassForm.Password1.value == document.PassForm.Password2.value)
      {
        alert("Good!");
      }
      else
      {
        alert("Wrong!");
      }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="PassForm">
    Enter your password: <INPUT NAME="Password1"><BR>
    Enter your password (again): <INPUT NAME="Password2"><BR>
    <INPUT TYPE="BUTTON" VALUE="Enter" ONCLICK="checkPass()">
    </FORM>
    </BODY>
    </HTML>

  6. #6
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I'm no javascript maestro, but I do know a fair bit of java. I believe your problem is the following (I could be wrong):

    Code:
    if (document.passwordForm.password.value)==(document.passwordForm.password2.value)
    {
    //load page
    }
    Look at your if statement, you have brackets around the variables, but not around your whole statement. If you tried to compile that with a java compiler, it probably wouldn't like that. The correct statement would be:

    Code:
    if(document.passwordForm.password.value == document.passwordForm.password2.value)
    {
      // load page
    }
    Tim, the reason that your code works isn't because of the <SCRIPT> tag, it's because it does not contain the same syntactical mistake. I also didn't have anything to do with not having declared the variables ic2, what djhuk was doing (AFAIK) was taking the text from two fields on an html form. And from the rest of the post, it sounded as if that had already been written...he just posted a code exerpt.

    Hope that solves your problem,

    ac

  7. #7
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I'm no javascript maestro, but I do know a fair bit of java. I believe your problem is the following (I could be wrong):

    Code:
    if (document.passwordForm.password.value)==(document.passwordForm.password2.value)
    {
    //load page
    }
    Look at your if statement, you have brackets around the variables, but not around your whole statement. If you tried to compile that with a java compiler, it probably wouldn't like that. The correct statement would be:

    Code:
    if(document.passwordForm.password.value == document.passwordForm.password2.value)
    {
      // load page
    }
    Tim, the reason that your code works isn't because of the <SCRIPT> tag, it's because it does not contain the same syntactical mistake. I also didn't have anything to do with not having declared the variables ic2, what djhuk was doing (AFAIK) was taking the text from two fields on an html form. And from the rest of the post, it sounded as if that had already been written...he just posted a code exerpt.

    Hope that solves your problem,

    ac

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    147
    thanks everyone and thanks gothic (you weren't wrong)

  9. #9
    Senior Member
    Join Date
    May 2002
    Posts
    147
    thanks everyone and thanks gothic (you weren't wrong)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •