Javascript code not working as required..

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
In my website i navigate a person from his profile to another page where he enters his old ,new password and retypes his new password..
I used a variable count to count the no of failed attempts...it is just not working...

Code:
function chngepwd()
{
if(count>3)
{
alert("Number of attempts allowed have been used.Please wait!!!");   
window.onbeforeunload = function () {
     return "This session is expired and the history altered.";
}
document.frmchgpwd.login.type="submit";
document.frmchgpwd.action="/Index.php";
document.frmchgpwd.submit();
}
var oldpass= document.frmchgpwd.txtoldpass.value ;
var newpass= document.frmchgpwd.txtnewpass.value ;
var rnewpass= document.frmchgpwd.txtrnewpass.value ;
var chkpass=document.frmchgpwd.txtchkpass.value ;
var count=0;
 if(oldpass==chkpass) 
 {
 if(newpass)
 {
 if(newpass==rnewpass)
 {
 document.frmchgpwd.txtchkpass.value=newpass ;
 document.frmchgpwd.login.type="submit" ; 
 document.frmchgpwd.action="Update.php";
 document.frmchgpwd.submit();
 }
 else
 {
  document.frmchgpwd.login.type="button" ;
 alert("Passwords dont match !!!.Try again");
 count=count+1;
  }
 }
 }
 else
 {
 document.frmchgpwd.login.type="button" ;
 alert("Wrong password entered.Try again !!!");
 count=count+1;
  }
 }
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Count is a local variable here, and even if it wasn't it's still being reset to 0 every time. You need to modify it a bit to something like this:

Code:
var count=0;

function chngepwd() {
    if(count>3) {
        alert("Number of attempts allowed have been used.Please wait!!!");   
        window.onbeforeunload = function () {
            return "This session is expired and the history altered.";
        }
        document.frmchgpwd.login.type="submit";
        document.frmchgpwd.action="/Index.php";
        document.frmchgpwd.submit();
    }
    
    var oldpass= document.frmchgpwd.txtoldpass.value ;
    var newpass= document.frmchgpwd.txtnewpass.value ;
    var rnewpass= document.frmchgpwd.txtrnewpass.value ;
    var chkpass=document.frmchgpwd.txtchkpass.value ;
    
    if(oldpass==chkpass)  {
        if(newpass) {
            if(newpass==rnewpass) {
                document.frmchgpwd.txtchkpass.value=newpass ;
                document.frmchgpwd.login.type="submit" ; 
                document.frmchgpwd.action="Update.php";
                document.frmchgpwd.submit();
            }
            else
            {
                document.frmchgpwd.login.type="button" ;
                alert("Passwords dont match !!!.Try again");
                count++;
            }
        }
    }
    else {
        document.frmchgpwd.login.type="button" ;
        alert("Wrong password entered.Try again !!!");
        count++;
    }
}
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
Hey woiwky thanks 4 that one ....it just did not com2 my mind , lolz

document.frmchgpwd.login.type="button" ;

well this code here gives an error in IE(cannot access type method ) but not in opera (works as intended)
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
It's probably a better idea to use

Code:
document.frmchgpwd.login.onclick = function () {
   return false;
}

to disable the button. Then if you want to enable it, you could just do:

Code:
document.form1.submit.onclick = '';
 
Top