Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question password validation?

    hello,
    how can i make sure that passwords in two input feilds are the same with our submitting to the server?

    I'm very new so an example would be great!

    thanks in advance for your time!

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can check using javascript, but this can be disabled. You might want to use a client side language such as PHP.
    Code:
    <head>
    <script>
    function checkForSamePassword(pw1,pw2){
    if (pw1 == pw2){
    alert('They are the same')//Do what you want if they are the same.
    }
    else{
    alert('They are not the same')//Do what you want if they are NOT the same.
    }
    }
    </script>
    </head>
    <body>
    <input type = "password" id="pass1" /><br />
    <input type = "password" id="pass2" /><br />
    <input type = "button" onclick = "checkForSamePassword(document.getElementById('pass1').value,document.getElementById('pass2').value)" />

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Here is what I use. It also checks for a "valid" password (only alphanumerics).
    Hope this is what you require. You must alter "regform" to the name of your own form.


    <SCRIPT language= "JavaScript">
    function testpw() {
    var a = document.regform.Password.value;
    var b = document.regform.Password2.value;
    var c = a.replace (/[^a-zA-Z0-9]/g,"XX");
    if ((a.length <6) || (c != b)) {
    alert ("Your two passwords are not the same, or \\nthey contain invalid characters, or \\nthey are less than 6 characters in length. \\n\\nPlease re-enter them! \\n\\nYour password is case sensitive \\n - ABC is not the same as abc.");
    document.regform.Password.value="";
    document.regform.Password2.value="";
    document.regform.Password.focus();
    return false;
    }
    }
    </SCRIPT>

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, just one more question,
    when using this script:

    <head>
    <script>
    function checkForSamePassword(pw1,pw2){
    if (pw1 == pw2){
    alert('They are the same')//Do what you want if they are the same.
    }
    else{
    alert('They are not the same')//Do what you want if they are NOT the same.
    }
    }
    </script>
    </head>
    <body>
    <input type = "password" id="pass1" /><br />
    <input type = "password" id="pass2" /><br />
    <input type = "button" onclick = "checkForSamePassword(document.getElementById('pass1').value,document.getElementById('pass2').value) " />


    it continues to submit the forum even if the password do not match, like i get the alert, but i would like it to stop wheni get the alert, and give me the chance to correct the problem.

    can this be done?

    thanks again for your time!

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    <html>
    <head>
    <script>
    function checkForSamePassword(f){
      if (f.pass1.value != f.pass2.value){
        alert('They are not the same');
        return false;
      }
      return true;
    }
    </script>
    </head>
    <body>
    <form name="myform" action="processpage.xxx" method="post" onsubmit="return checkForSamePassword(this)">
    <input type = "password" id="pass1" name="pass1" /><br />
    <input type = "password" id="pass2" name="pass2" /><br />
    <input type = "submit" name="btnSubmit" value="Submit" />
    </form>
    </body>
    </html>

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    thanks!

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    With respect glenngv,

    function checkForSamePassword(f){
    if (f.pass1.value != f.pass2.value){
    alert('They are not the same');
    return false;
    }
    return true;
    }

    allows the user to enter anything, even a space as a password. Or in fact enter nothing at all - the function will still return true.

    IMHO it is better to positively check for alphnumerics only, and a
    defined minimum password length (say 6 characters).

    Also, if the passwords do not match the two boxes should be cleared and the user invited to enter them again (focus). As it is he is simply told that ******* does not match *******. Where does he go from there?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I agree with you but the original poster just ask for a script that checks 'password and confirm password' match. He/She did not mention about verifying against password policy such as minimum password length, password restrictions, etc. I just presented a basic solution. It's up to him/her to enhance it according to additional (if any) requirements.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    glenngv - I can't think why, but I somehow get the impression that you work in the public sector.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    What do you mean by that?
    Is it a compliment or not?

    BTW, I work in a privately owned multinational IT company as a Web developer/analyst.


  •  

    Posting Permissions

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