Click here to Skip to main content
Licence Ms-PL
First Posted 15 Aug 2006
Views 71,668
Downloads 1,033
Bookmarked 33 times

Applying a watermark to a textbox in a Web Application

By | 29 Aug 2006 | Article
An article on using JavaScript in ASP.NET for applying a watermark to a textbox in a Web Application.

Introduction

You may have seen some web sites displaying watermarks in text boxes. As soon as you click on a text box, the watermark disappears and you can start entering text. In this article, I will explain a small JavaScript trick that you can use to do the same in your web application. I have included a library that you can use to achieve the same functionality without having to worry about writing JavaScript code.

Sample screenshot

Background

The idea is to capitalize on the OnFocus and OnBlur events of the HTML input tag. The library sets flips between two styles for the input tag and, also, it checks if the default text has been changed or not.

Using the code

To use the code, you need to provide at least two styles for the text box. One that represents the watermarked textbox and another that represents a normal textbox. The sample project includes such a stylesheet.

To apply watermark to a textbox with id=textbox1, write the following in your page_load event handler:

TextBoxWatermark.WaterMarkHelper.ApplyWaterMarkToTextBox(textBox1, _
   "Watermarktext", "WaterMarkedTextBoxStyle", "NormalTextBoxStyle")

Enhancements

You can enhance the library to intercept the submit event and remove the default text before the required field validators fire.

Update history

  • August 30, 2006: Patched the single quote bug. The library used to break down when a single quote was supplied in WatermarkText.
  • August 23, 2006: Patched the library to work with Firefox.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Deobrat Singh

Software Developer
Microsoft India
India India

Member

I am a developer (okay... that's very obvious!). I started programming at the age of 17 when I entered my engineering graduation back in the year 2001. I started off with C, C++ on UNIX. But soon I realized that if I need to make money out of my skills, I need to add some visual element to my applications (and that I need to switch to MS technologies!). Someone told me about VB. I started learning it but couldn’t get thru (I was religiously in love with curly braces; I never felt VB is a language). Then I began with VC++ 6.0 and I fell in love with it. Since then, I have been working on various MS technologies (C++, C#, VB.NET, SQL Server 2000/2005 etc.) I graduated in 2005 and currently, I am working with Microsoft in Hyderabad, India.
That’s all I can write as of now. If you want to know more, drop me a mail, I will try and respond Smile | :)

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralC# Version PinmemberMember 76949321:53 21 Mar '11  
GeneralNEED IN C# Pinmembersuryansh4u10:38 21 Sep '10  
GeneralMy vote of 5 Pinmembersuryansh4u10:36 21 Sep '10  
Generalhi Pinmembermanishkumarsoni16:04 1 Jul '10  
GeneralPassword boxes PinmemberJohnny J.23:53 9 Mar '09  
QuestionMissing Library in Source Zip PinmemberDapo Onawole0:07 19 Nov '08  
AnswerRe: Missing Library in Source Zip PinmemberDeobrat Singh0:17 19 Nov '08  
GeneralRe: Missing Library in Source Zip PinmemberDapo Onawole1:22 19 Nov '08  
GeneralRe: Missing Library in Source Zip PinmemberDeobrat Singh1:24 19 Nov '08  
GeneralC# Version PinmemberBronwenZande15:42 2 Jan '07  
GeneralASP.NET 2.0 ISSUE PinmemberRogerMCT7:34 23 Oct '06  
AnswerRe: ASP.NET 2.0 ISSUE PinmemberDeobrat Singh18:01 23 Oct '06  
QuestionHow to customize the messages while setup the application using vb.net? Pinmemberxxxxxxxxxchandra0:43 30 Aug '06  
QuestionWatermark library and single quotes Pinmemberpbishop9:10 29 Aug '06  
AnswerRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh9:29 29 Aug '06  
GeneralRe: Watermark library and single quotes Pinmemberpbishop9:49 29 Aug '06  
GeneralRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh10:06 29 Aug '06  
GeneralRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh19:47 29 Aug '06  
GeneralRe: Watermark library and single quotes Pinmemberpbishop3:30 30 Aug '06  
GeneralAtlas can do this too with control extenders PinmemberRobert Ensor20:12 22 Aug '06  
GeneralRe: Atlas can do this too with control extenders [modified] PinmemberDeobrat Singh20:46 22 Aug '06  
QuestionValidators? Pinmemberrjpaulsen9:20 22 Aug '06  
AnswerRe: Validators? PinmemberDeobrat Singh18:29 22 Aug '06  
Yeah... You are right. As I have pointed out in the Enhancement section, it is definitely required to make this component work with validation controls. I am already working on it. And you would be happy (i guess so) to know that I am working on the second suggestion that you gave (a script removing the default data).
Thanks for the comment. And expect a revision of this article (with an improved version of the component) in a week or two (I dont get much time to do it. I'll try my best).
 
Deobrat Singh


Software Engineer,
HCL Technologies Ltd.
Mumbai, India

Education is what survives when what has been learned has been forgotten.
- B. F. Skinner

GeneralRe: Validators? Pinmemberitsmaryann0:04 8 Sep '08  
GeneralFireFox Problem Pinmemberasifrazach4:19 22 Aug '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 30 Aug 2006
Article Copyright 2006 by Deobrat Singh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid