473,796 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clear a textbox on GotFocus in a web-page ?

Hi.

I have... well.... not very many computer-literate users to my web-
site.

For the textbox which expects an email address, for instance, I had to
enter a default text like
"Email..." because if not, they phoned me asking questions like "What
does the message 'Your email address is required' ?" :-)))

Now the problem is that many users don't erase this default text, and
enter their email address AFTER the default text, so that the contents
of the textbox is now
"Email...my**** @myISP.com"

On this textbox,I have, of course, a RequiredFieldVa lidator, another
RequiredFieldVa lidator (with InitialValue="E mail...") and a
RegularExpressi onValidator (with the regex
ValidationExpre ssion="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-
\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

But this does not filter out the "Email... " text.

I *could* alter the ValidationExpre ssion, of course, but I would want
a more elegant solution, one which would clear the default text out on
a textbox on GOTFOCUS, and conversely, put the default text back on
LOSTFOCUS, if, of course, the text in that text-box has not been
changed from the default - if the text HAS been changed, then,
obviously, it has to be left there.

So, for example, say that txtEmail has the default contents
"Email...". You click in it, the text disappears. You click away, it
becomes "Email..." again. You click in it again, it becomes null
again. You type "mm*@mmm.co m" and you click away, the text stays
"mm*@mmm.co m".

I'm sure that this requires some JavaScript, but.... I don't know
enough JavaScript for this, unfortunately. Could you please direct me
to such a piece of code ?

Thank you very much.
Alex.

Jul 17 '07 #1
1 4491
It is done with JavaScript. You have an onFocus event handler you can add to
the tag. Send it to a routine that clears the value of the object.

onFocus="JavaSc ript:clearContr ol(this);"

which fires an event like:

function clearControl(cn trl)
{
cntrl.Value = "";
}

I wrote the above on the fly, so it might not be properly formatted, but it
gives the basic idea. If you want to put Email back if they hover off
without filling it in, you can use the onBlur event.

onBlur="JavaScr ipt:ResetContro l(this, 'Email');"

function ResetControl(cn trl, message)
{
if(cntrl.Value == '')
{
cntrl.Value = message;
}
}

Once conquered, the above is highly configurable

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

*************** *************** *************** ***
Think outside the box!
*************** *************** *************** ***
"Radu" <cu************ *@yahoo.comwrot e in message
news:11******** **************@ z28g2000prd.goo glegroups.com.. .
Hi.

I have... well.... not very many computer-literate users to my web-
site.

For the textbox which expects an email address, for instance, I had to
enter a default text like
"Email..." because if not, they phoned me asking questions like "What
does the message 'Your email address is required' ?" :-)))

Now the problem is that many users don't erase this default text, and
enter their email address AFTER the default text, so that the contents
of the textbox is now
"Email...my**** @myISP.com"

On this textbox,I have, of course, a RequiredFieldVa lidator, another
RequiredFieldVa lidator (with InitialValue="E mail...") and a
RegularExpressi onValidator (with the regex
ValidationExpre ssion="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-
\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

But this does not filter out the "Email... " text.

I *could* alter the ValidationExpre ssion, of course, but I would want
a more elegant solution, one which would clear the default text out on
a textbox on GOTFOCUS, and conversely, put the default text back on
LOSTFOCUS, if, of course, the text in that text-box has not been
changed from the default - if the text HAS been changed, then,
obviously, it has to be left there.

So, for example, say that txtEmail has the default contents
"Email...". You click in it, the text disappears. You click away, it
becomes "Email..." again. You click in it again, it becomes null
again. You type "mm*@mmm.co m" and you click away, the text stays
"mm*@mmm.co m".

I'm sure that this requires some JavaScript, but.... I don't know
enough JavaScript for this, unfortunately. Could you please direct me
to such a piece of code ?

Thank you very much.
Alex.

Jul 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4578
by: fmarchioni | last post by:
Hi all, I need to highlight textbox in a Form when they are selected. In order to do it I have added for each one a .GotFocus and .LostFocus Event Handler.... textRS.GotFocus += new EventHandler(gotFocus_Event); textName.GotFocus += new EventHandler(gotFocus_Event); textSurname.GotFocus += new EventHandler(gotFocus_Event); textAddress.GotFocus += new EventHandler(gotFocus_Event);
2
4461
by: Gopal Krishna Tengli | last post by:
Hi, I want to impliment the textbox's gotfocus event. Its not a intrincic event. So not listed in the editor. Waiting for an innovative reply... Gopal Krishna
10
9052
by: Jane Sharpe | last post by:
Hi, I have a textbox with the words "Enter your name here" inserted as default text - At the moment, to remove this the user must highlight all the text and delete before they type in their name - I've seen sites where all this text dissapears as soon as the user clicks on it - how do I do that ? Thanks Jane
12
1725
by: Gene Hubert | last post by:
How do I make a TextBox behave like the address bar in IE? That is... If focus is not on the tb, select all text if clicking on the tb or tabbing into the tb. Clicking again deselects the text and sets the insertion point to where clicked. I've tried several options inheriting from TextBox but none work quite right. Thanks Much,
10
3131
by: Dennis | last post by:
I have a simple form with one button and one text box. In the Form, I create an array list to track the events by adding a descriptive string item to the arraylist in each event. I first Click on the Button then Click on the TextBox and enter an "a" then click on the Button again. The following is what I get for the event tracking Arraylist: but - GotFocus but - Clicked but - LostFocus txt - Changed
2
8044
by: Jonathan Allen | last post by:
In dialog boxes in Windows, the text in TextBox is automatically selected if you tab into the TextBox , but not when you select the TextBox using the mouse. What is the preferred way to support this in VB/C# 2005? -- Jonathan Allen
8
21902
by: cj | last post by:
I asked this question a couple of days ago but am just now looking at it again. I used to use the textbox gotfoucs event to have all the text in the textbox selected when it gotfocus. That doesn't seem to work in .net when the textbox receives focus via a mouse click. Jeffrey and Shane both advised how to get a mouse click to select all the text (thank you both) but using the mousedown or mouseup events doesn't work the way I want it...
5
7460
by: =?Utf-8?B?QmVuIFIu?= | last post by:
Hi, In a .NET 2.0 winforms application, I've got a textbox that, when updated, uses the validated event to cascade the change to another textbox (along with another value). This works well if the user does indeed move the cursor to another textbox, but if the user clicks my "done" toolstripbutton after making a change in the textbox but without moving the cursor out, the validated event never fires. I have a pretty bad workaround where...
7
1614
by: Localbar | last post by:
Hi all, In my form have more then 10 textbox. I would like to make all textbox when lostfocus backcolor is white, when gotfocus backcolor is yellow. But I don't want to write same code in the form. How to solve this problem. Is it can make a textbox class to control or some other method? Thanks
10
18051
by: engteng | last post by:
When textbox properties enable = False the font in the textbox become gray color. How do I change the gray color to black color ? Regards, Tee
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.