Click here to Skip to main content
15,891,745 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have two text box One Password and one Confirm Password text box.After entering the
password and confirm password. When Password text box value is deleted and when
submit button is clicked then it should also delete the Confirm Password value.The code
is given below....

JavaScript
var valpassword= document.getElementById("Password").value;
var valconfirm= document.getElementById("Confirm").value;
if(valpassword =="")
    {
        document.getElementById("spnPassword").innerHTML = "Please enter your Password";
    }
    if(valconfirm =="")
    {
        document.getElementById("spnConfirm").innerHTML = "Please confirm your Password";
    }
    else if(valpassword!=valconfirm)
    {
        document.getElementById("spnConfirm").innerHTML = "Your passwords do not match ";
    }
    else if(document.getElementById("Password").value=="" &&  document.getElementById("Confirm").value!="")
    {
        document.getElementById("Confirm").value="";
    }
    return true;
Posted
Updated 20-Jan-11 19:58pm
v4

Something like this:

JavaScript
function deleteConfirmPasswordText()
{
    if(document.getElementById("txtPassword").value == "") {
        document.getElementById("txtConfirmPassword").value = "";
    }
    return false;
}


XML
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return deleteConfirmPasswordText();" />


Note: return false is because the form shouldn't post if the password text box is empty. You must have these validations in place, both at client side and server side.

Hope this helps!
 
Share this answer
 
v2
Comments
Lyn123 21-Jan-11 1:44am    
Can you check the code.....
Ankur\m/ 21-Jan-11 1:48am    
What do you mean by that? If you meant if the code I gave is valid, yes it is. I checked it.
Lyn123 21-Jan-11 1:54am    
I mean my code in the question......
Ankur\m/ 21-Jan-11 2:06am    
Couple of things that need to be changed:
a) Add return false; inside all 'if' statements.
b) the first if-else would be only 'if'. Or make all of them only 'if' and it will work.

Since the passwords do not match, the first if-else statement is executed and never goes to the last if-else and thus confirm text box doesn't get cleared.

I hope that clears your doubt.
Lyn123 21-Jan-11 2:03am    
else if(document.getElementById("Password").value=="" && document.getElementById("Confirm").value!="")
{
document.getElementById("Confirm").value="";
}

or

else if(valpassword=="" && valconfirm!="")
{
document.getElementById("valconfirm").value="";
}
My this code is not working.......
You need to set the textbox text property to null or "".
 
Share this answer
 
v2
Comments
Ankur\m/ 21-Jan-11 1:49am    
OT: Your newly styled name looks great! :)
Abhinav S 21-Jan-11 4:55am    
It happened automatically....some changes on CP. I did not do anything. :)
Ankur\m/ 21-Jan-11 5:17am    
Really?! But why you only? ;)
Abhinav S 21-Jan-11 6:10am    
I've got the kbd tag in my name. So i guess some setting on CP has changed.
Ankur\m/ 21-Jan-11 6:23am    
Oh, I see. I thought you did something. :doh:
And hey I didn't know about kbd tag. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900