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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Request for Javascript code for comparing two passwords in two different textboxes

    I request you send me Javascript code for comparing two passwords in two different textboxes

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    727
    Thanks
    35
    Thanked 132 Times in 123 Posts
    This will work if you have exactly two password fields:

    Code:
    function comparepasswords(){
    	var A= document.getElementsByTagName('input');
    	var L= A.length, i= 0,tem,P= [];
    	while(i<L){
    		tem= A[i++];
    		if(tem.type.toLowerCase()== 'password')P[P.length]=tem;
    	}
    	if(P[0].value && P[0].value=== P[1].value)return true;
    	else return false;
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Javascript is totally unsuitable for password/logon as the source code can be read by anyone.

    Having said that:-

    Code:
    <form name = "myform">
    <input type = "text" name = "pwd1" id = "pwd1"  size =  "12"><br>
    <input type = "text" name = "pwd2" id = "pwd2"  size =  "12"><br>
    <input type = "button" value = "Click here to verify password" onclick = "pwdCompare()">
    </form>
    
    <script type = "text/javascript">
    
    function pwdCompare() {
    var p1 = document.myform.pwd1.value;
    p1 = p1.replace(/^\s+/g, "");  // strip leading spaces
    var p2 = document.myform.pwd2.value;
    p2 = p2.replace(/^\s+/g, "");  // strip leading spaces
    if ((p1.length < 6) || (p1 != p2)) {  // minimum 6 characters
    alert ("The two passwords do not match or have too few characters!  Try again!");
    document.myform.pwd1.value = "";
    document.myform.pwd2.value = "";
    document.myform.pwd1.focus();
    return false;
    }
    else {
    alert ("The passwords match!");
    }
    }
    
    </script>
    All this does is checks that the two passwords match (case sensitive) and have a minimum of 6 characters. You will need further verification if the passwords have to have a minimum number of alpha characters, or some combination of letters/numbers/special characters.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 06-11-2008 at 07:07 PM.


  •  

    Posting Permissions

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