Click here to Skip to main content
Licence Ms-PL
First Posted 16 Aug 2006
Views 70,434
Downloads 954
Bookmarked 31 times

Applying a watermark to a textbox in a Web Application

By Deobrat Singh | 29 Aug 2006
An article on using JavaScript in ASP.NET for applying a watermark to a textbox in a Web Application.
1 vote, 6.3%
1
3 votes, 18.8%
2
2 votes, 12.5%
3
4 votes, 25.0%
4
6 votes, 37.5%
5
3.72/5 - 16 votes
1 removed
μ 3.39, σa 2.37 [?]

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 76949322:53 21 Mar '11  
GeneralNEED IN C# Pinmembersuryansh4u11:38 21 Sep '10  
GeneralMy vote of 5 Pinmembersuryansh4u11:36 21 Sep '10  
Generalhi Pinmembermanishkumarsoni17:04 1 Jul '10  
GeneralPassword boxes PinmemberJohnny J.0:53 10 Mar '09  
QuestionMissing Library in Source Zip PinmemberDapo Onawole1:07 19 Nov '08  
AnswerRe: Missing Library in Source Zip PinmemberDeobrat Singh1:17 19 Nov '08  
GeneralRe: Missing Library in Source Zip PinmemberDapo Onawole2:22 19 Nov '08  
Yeah right! Thanks. Missed that folder while in a hurry to get this task done. Laugh | :laugh:
GeneralRe: Missing Library in Source Zip PinmemberDeobrat Singh2:24 19 Nov '08  
GeneralC# Version PinmemberBronwenZande16:42 2 Jan '07  
GeneralASP.NET 2.0 ISSUE PinmemberRogerMCT8:34 23 Oct '06  
AnswerRe: ASP.NET 2.0 ISSUE PinmemberDeobrat Singh19:01 23 Oct '06  
QuestionHow to customize the messages while setup the application using vb.net? Pinmemberxxxxxxxxxchandra1:43 30 Aug '06  
QuestionWatermark library and single quotes Pinmemberpbishop10:10 29 Aug '06  
AnswerRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh10:29 29 Aug '06  
GeneralRe: Watermark library and single quotes Pinmemberpbishop10:49 29 Aug '06  
GeneralRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh11:06 29 Aug '06  
GeneralRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh20:47 29 Aug '06  
GeneralRe: Watermark library and single quotes Pinmemberpbishop4:30 30 Aug '06  
GeneralAtlas can do this too with control extenders PinmemberRobert Ensor21:12 22 Aug '06  
GeneralRe: Atlas can do this too with control extenders [modified] PinmemberDeobrat Singh21:46 22 Aug '06  
QuestionValidators? Pinmemberrjpaulsen10:20 22 Aug '06  
AnswerRe: Validators? PinmemberDeobrat Singh19:29 22 Aug '06  
GeneralRe: Validators? Pinmemberitsmaryann1:04 8 Sep '08  
GeneralFireFox Problem Pinmemberasifrazach5: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
Web02 | 2.5.120210.1 | Last Updated 30 Aug 2006
Article Copyright 2006 by Deobrat Singh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid