Results 1 to 4 of 4

Thread: [RESOLVED] check if variables match?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] check if variables match?

    i need to check if the contents of 2 textboxes match and i load the contents of these 2 into a variable
    Code:
    var pass = document.getElementById('password').value;
    var cpass = document.getElementById('cpassword').value;
    correct the code if its wrong, because i'm kinda new at javascript, my friend told me to stay away from it if it was possible xD
    so what i need to do is check if those 2 textboxes value match, how to do this?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: check if variables match?

    Code:
    if(pass == cpass){
    
      alert('they match.');
    
    }

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: check if variables match?

    Just a more verbose example
    Code:
    window.onload = function(){
      document.getElementById("myForm").onsubmit = function (){
        var pass = document.getElementById('password').value;
        var cpass = document.getElementById('cpassword').value;
        
        if(pass == cpass){
          alert("They're the same!");
        }else{
          alert("They're not the same.");
        }
        
        return false;
      }
    }
    This assumes that you've given your form an id of "myForm", and that you want the comparison to be done when the person submits the form. You would need to alter this code because it always returns false to the form (so it will never submit).

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: check if variables match?

    thanks Kows and Samba i only needed the code for checking, the rest i would like to do my self, and so i did:

    Code:
    function checkpassword() {
    
    var pass = document.getElementById('password').value;
    var cpass = document.getElementById('cpassword').value;
    if(pass == cpass){
      document.getElementById('passresult').innerHTML = "Passwords match";
    } else {
      document.getElementById('passresult').innerHTML = "Passwords do not match";
    }
    
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width