Groups > Asp .Net > ASP.NET Tips and tricks > Re: How to change textmode of Textbox from text to password in JavaScript?




How to change textmode of Textbox from text to password in
JavaScript?

How to change textmode of Textbox from text to password in JavaScript?
Thu, 3 Jan 2008 10:56:24 +0000
We can not change the textmode of textbox in JavaScript . Because  textmode  is
design property.

But we can do it in other way. 

On page load

TextBox1.Attributes.Add("onfocus", "if(this.value.toLowerCase()
== 'password'){document.getElementById('DivPwdTxt').style.display = 'none';
document.getElementById('Divpwd').style.display='inline';document.getElementById
('ctl00_Password').focus(); }");
        Password.Attributes.Add("onBlur", "if(this.value ==
''){document.getElementById('Divpwd').style.display='none';
document.getElementById('DivPwdTxt').style.display = 'inline';}");

 

UserName.Attributes.Add("onfocus", "if(this.value.toLowerCase()
== 'userid') this.value = '';");
        UserName.Attributes.Add("onBlur", "if(this.value == '')
this.value = 'UserId';");
 

and on 

.aspx page

<asp:Label ID="Label2" runat="server"
CssClass="label1" Text="UserId" Width="50 px"
></asp:Label>
                                        <asp:TextBox runat="server"
ID="UserName" CssClass="label1" Text="UserId"
Width="120 px" ></asp:TextBox>
                                        
                                         <asp:Label ID="Label3"
runat="server" CssClass="label1" Text="Password"
Width="50 px"></asp:Label>
                                         <div id="DivPwdTxt">
                                        <asp:TextBox runat="server"
ID="TextBox1" CssClass="label1" Width="120 px"
Text="Password"
AutoPostBack="false"></asp:TextBox>
                                        </div>
                                        <div id="Divpwd"
style="display:none;">
                                        <asp:TextBox runat="server"
ID="Password" TextMode="Password"
CssClass="label1" Width="120 px"
AutoPostBack="false"></asp:TextBox>
                                        </div>
Post Reply
Re: How to change textmode of Textbox from text to password in JavaScript?
Sat, 19 Jan 2008 04:50:02 +000
Hi,

I am asuming you wan't to change the input to be a textbox when entering
password and a password field when you get out of it. To do so you can use
javascript to change the type of the field.

I.E. 

    <input type="password" id="bob"
onfocus="this.type='text';" onblur="this.type='password';"
/>

So when you enter the textbox you would see text and on exit it sets back to
password. With javascript off it woudl just be a normal password box. To do this
in you .net code you could use a textbox control on the page like this:

<asp:TextBox ID="txt1"
TextMode="Password"></asp:TextBox>

Then in your page load add the attributes:

txt1.Attributes.Add("onfocus", "this.type='text';");
txt1.Attributes.Add("onblur", "this.type='password';");


I think this will give you the affect you are after. 

 

Thanks

Stefan
Post Reply
Re: How to change textmode of Textbox from text to password in JavaScript?
Mon, 21 Jan 2008 09:38:24 +000
Sry My Dear freind,

but there is a javascript error."htmlfile: Could not get the type
property"
Post Reply
Re: How to change textmode of Textbox from text to password in JavaScript?
Mon, 21 Jan 2008 11:01:33 +000
What browser are you using? Post code if possible ill take a look.

 

Thanks
Stefan
Post Reply
Re: How to change textmode of Textbox from text to password in JavaScript?
Tue, 22 Jan 2008 09:24:32 +000
code is same as you are posted. it's working on mozilla but not on IE7.

TxtBox.Attributes.Add("onfocus", "if(this.value.toLowerCase() ==
'password'){this.value = ' ' ; this.type='password';}");
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact