Click here to Skip to main content
Click here to Skip to main content

TextBox Watermark Using JavaScript and CSS

By , 29 May 2009
 

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.

 <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>
 <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> 
<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)

About the Author

Md.Asaduzzaman Azad
Data Path Limited
Bangladesh Bangladesh
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membertuhincse249 Oct '12 - 20:22 
It helps me very much
QuestionTextMode="Password"memberArin.Net8 Jul '12 - 23:39 
When you set TextMode="Password" even watermark text is getting displayed like password (a dot for each letter), what is your suggestion for solving this?
GeneralMy vote of 4memberArin.Net8 Jul '12 - 23:36 
I believe this is much more practical and light weight than using ASP.NET AJAX Control toolkit Textbox Watermark extender! specially when login section is part of Master Page.
GeneralMy vote of 4memberLee Reid26 Mar '12 - 15:09 
Useful, thanks!
As an aside, if you are using JQuery, you can cut this code right down by simply adding and removing classes, rather than reassigning entire classes just for the sake of text colour.
GeneralMy vote of 5memberodhin22 Sep '11 - 23:25 
Good job!!! thanks.
GeneralMy vote of 5memberzhangtai12 Dec '10 - 16:22 
nice job!
Generalvalidatorsmemberthebts28 Dec '09 - 0:12 
How do you deal with asp.net validator controls?
I can see a way with required field valididators by setting the initial text the same as the water mark text, but how about other validators?
Question????memberJohnny J.29 May '09 - 3:36 
Code? Demo? Screenshot?
AnswerRe: ????memberMd.Asaduzzaman Azad3 Jun '09 - 18:26 
I can't understand your comments.Anything wrong!
AnswerRe: ????membermusacj9 Jun '09 - 6:12 
Johnny G... the code is there, you just need to copy and paste in your own project or files; what's missing is a screenshot which could be useful to someone who doesn't understand what the article is talking about. Sniff | :^)
 
To the author: I always wondered how this is done but never had time or a reason to do it on my own... thanks, that helped me a lot. Smile | :)
 
Mr MC; The Only One

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 29 May 2009
Article Copyright 2009 by Md.Asaduzzaman Azad
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid