Thursday, 3 May 2012

How to put Watermark for textboxes | Watermark for Username and Password | Watermark in Javascript

                      Hi Friends, in this article I will explain about Watermark for Username and Password in JavaScript.
I already explained in the previous articles about Marquee tag or How to Scroll Text From left to right or How to Move the Text in HTML,C#/VB.NET:Save the generated pdf directly to the server directory folder without user prompt in ASP.NET and How to open PDF File in Adobe Reader, not in Browser in ASP.NET using C#/VB.NET
                      We may see Watermark textboxes  in so many sites. Suppose we take the twitter  site in login page and signup page it contains Watermark textboxes  .
                             Watermark is not working properly for the password if we take the Textmode as Password(TextMode="Password").For  that i take the extra textbox txtTempPwd and write the code as following.
In previous post i explained about how to create ASP.NET website.
Take one web page(.aspx page)
Copy and Paste the below code in your web page.
<html xmlns="">
<head id="Head1" runat="server">
<title>Watermark Textboxs for username and Password  Using JavaScript</title>
<script language="javascript" type="text/javascript">
    function WaterMark(objtxt, event) {
        var defaultText = "Username";
        var defaultpwdText = "Password";
        // Condition to check textbox length and event type
        if ( == "txtUserName" || == "txtPwd") {
            if (objtxt.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                if ( == "txtUserName") {
           = "Gray";
                    objtxt.value = defaultText;
                if ( == "txtPwd") {
                    document.getElementById("<%= txtTempPwd.ClientID %>").style.display = "block";
           = "none";
        // Condition to check textbox value and event type
        if ((objtxt.value == defaultText || objtxt.value == defaultpwdText) & event.type == "focus") {
            if ( == "txtUserName") {
       = "black";
                objtxt.value = "";
            if ( == "txtTempPwd") {
       = "none";
            document.getElementById("<%= txtPwd.ClientID %>").style.display = "";
                document.getElementById("<%= txtPwd.ClientID %>").focus();
<form id="form1" runat="server">
<asp:TextBox ID="txtUserName" runat="server" Text="Username" Width="150px" ForeColor="Gray" onblur = "WaterMark(this, event);" onfocus = "WaterMark(this, event);" />
<asp:TextBox ID="txtTempPwd" Text="Password" runat="server" onfocus="WaterMark(this, event);" Width="150px" ForeColor="Gray" />
<asp:TextBox ID="txtPwd" TextMode="Password" Text="Password" runat="server" Width="150px" Style="display:none" onblur="WaterMark(this, event);"/>

Then the output like below.
when we click on the textboxes then the text will disappear like below.
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."


  1. Hi Roja,
    Really nice..
    Thanks for sharing…

  2. i have one doubt,here we using two textbox for password,what purpose we used two box,then how is possible to view one textbox only after execution


© 2012-2018 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.