#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    320
    Rep Power
    13

    Javascript anomoly - comparing two string values fails even on identical strings


    I have a profile edit form where a use can change their password. I'm letting javascript check for a consistant new password to cut down on unnecessary bandwidth.

    Here is the form: http://www.georgiaoffroad.com/form.html

    For convenience, here is the javascript from the same page:

    PHP Code:
            <SCRIPT LANGUAGE="JavaScript">

            function 
    CheckForm(theForm) {
                
    un = new String(theForm.n_username.value);
                
    cp = new String(theForm.c_password.value);
                
    np = new String(theForm.n_password.value);
                
    nnp = new String(theForm.nn_password.value);

                if (
    un=="") {
                    
    alert("You must enter a username");
                    
    theForm.n_username.focus();
                    return 
    false;
                }

                if (
    np!="" && nnp=="") {
                    
    alert("Please enter your new password a second time.");
                    
    theForm.nn_password.focus();
                    return 
    false;
                } else if (
    np=="" && nnp!="") {
                    
    alert("Please enter your new password a second time.");
                    
    theForm.n_password.focus();
                    return 
    false;
                }

                
    alert ("np " theForm.n_password.value);
                
    alert ("nnp " theForm.nn_password.value);
                if ( 
    np != nnp ) {
                    
    alert("New passwords do not match.  Please try again.");
                    
    theForm.n_password.select();
                    
    theForm.n_password.focus();
                    return 
    false;
                }

                if (
    cp=="" && np!="" || cp==""&& nnp!="") {
                    
    alert("You must enter your current password to make these changes");
                    
    theForm.c_password.focus();
                    return 
    false;
                }

            }
        
    </script> 
    Any ideas why this is happening?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    320
    Rep Power
    13
    I found a solution though I'm not sure what it happening that causes the script to work now.

    I changed the line

    if (np!=nnp)

    to

    if ( theForm.n_password.value != theForm.nn_password.value )

    Apparently declaring the np and nnp vars as strings messed things up. Problem is solved.

IMN logo majestic logo threadwatch logo seochat tools logo