Click here to Skip to main content
15,885,546 members
Articles / Web Development / XHTML

TextBox Watermark Using JavaScript and CSS

Rate me:
Please Sign up or sign in to vote.
4.64/5 (10 votes)
29 May 2009CPOL 105.7K   28   11
Apply watermark in TextBox using JavaScript

Introduction

In this article, I will explain how watermark textbox using JavaScript and CSS is very useful and uses less resources compared to AJAX.

Overview

JavaScript is used to implement TextBox ‘onfocus’ and ‘onblur’ event and CSS is used to decorate TextBox.

Using the Code

a.Bind the onfucus event to remove watermark and the onblur event to decorate textbox as watermark.

CSS
<style type="text/css">
       .WaterMarkedTextBox
       {
           height: 16px;
           width: 168px;
           padding: 2px 2 2 2px;
           border: 1px solid #BEBEBE;
           background-color: #F0F8FF;
           color: gray;
           font-size: 8pt;
           text-align: center;
       }
       .WaterMarkedTextBoxPSW
       {
           background-position: center;
           height: 16px;
           width: 168px;
           padding: 2px 2 2 2px;
           border: 1px solid #BEBEBE;
           background-color: #F0F8FF;
           color: white;
           vertical-align: middle;
           text-align: right;
           background-image: url(Images/psw_wMark.png);
           background-repeat: no-repeat;
       }
       .NormalTextBox
       {
           height: 16px;
           width: 168px;
       }
   </style>
JavaScript
<script language="javascript" type="text/javascript">
       function Focus(objname, waterMarkText) {
           obj = document.getElementById(objname);
           if (obj.value == waterMarkText) {
               obj.value = "";
               obj.className = "NormalTextBox";
               if (obj.value == "User ID" || obj.value == "" || obj.value == null) {
                   obj.style.color = "black";
               }
           }
       }
       function Blur(objname, waterMarkText) {
           obj = document.getElementById(objname);
           if (obj.value == "") {
               obj.value = waterMarkText;
               if (objname != "txtPwd") {
                   obj.className = "WaterMarkedTextBox";
               }
               else {
                   obj.className = "WaterMarkedTextBoxPSW";
               }
           }
           else {
               obj.className = "NormalTextBox";
           }

           if (obj.value == "User ID" || obj.value == "" || obj.value == null) {
               obj.style.color = "gray";
           }
       }
   </script>
ASP.NET
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            Watermark Textbox using JavaScript and CSS</h3>
    </div>
    <table>
        <tr>
            <td>
                User Id
            </td>
            <td>
                <asp:TextBox ID="txtUserId" runat="server" 
		onfocus="Focus(this.id,'User ID')"
                    onblur="Blur(this.id,'User ID')" 
		    Width="126px" CssClass="WaterMarkedTextBox">User ID</asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <asp:TextBox ID="txtPwd" TextMode="Password" 
		runat="server" onfocus="Focus(this.id,'')"
                    onblur="Blur(this.id,'')" Width="126px" 
			CssClass="WaterMarkedTextBoxPSW" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" />
            </td>
        </tr>
    </table>
    </form>
</body> 

History

  • 29th May, 2009: Initial post

License

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


Written By
Data Path Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tuhin.Towhidul9-Oct-12 20:22
professionalTuhin.Towhidul9-Oct-12 20:22 
QuestionTextMode="Password" Pin
Arin.Net8-Jul-12 23:39
Arin.Net8-Jul-12 23:39 
GeneralMy vote of 4 Pin
Arin.Net8-Jul-12 23:36
Arin.Net8-Jul-12 23:36 
GeneralMy vote of 4 Pin
Lee Reid26-Mar-12 15:09
Lee Reid26-Mar-12 15:09 
GeneralMy vote of 5 Pin
dabaza22-Sep-11 23:25
dabaza22-Sep-11 23:25 
GeneralMy vote of 5 Pin
zhangtai12-Dec-10 16:22
zhangtai12-Dec-10 16:22 
Generalvalidators Pin
thebts28-Dec-09 0:12
thebts28-Dec-09 0:12 
Question???? Pin
Johnny J.29-May-09 3:36
professionalJohnny J.29-May-09 3:36 
AnswerRe: ???? Pin
Md.Asaduzzaman Azad3-Jun-09 18:26
Md.Asaduzzaman Azad3-Jun-09 18:26 
AnswerRe: ???? Pin
musacj9-Jun-09 6:12
musacj9-Jun-09 6:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.