473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 RequiredFieldValidator, another
RequiredFieldValidator (with InitialValue="Email...") and a
RegularExpressionValidator (with the regex
ValidationExpression="^([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 ValidationExpression, 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.com" and you click away, the text stays
"mm*@mmm.com".

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 4454
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="javascript:clearControl(this);"

which fires an event like:

function clearControl(cntrl)
{
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="javascript:ResetControl(this, 'Email');"

function ResetControl(cntrl, 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.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.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 RequiredFieldValidator, another
RequiredFieldValidator (with InitialValue="Email...") and a
RegularExpressionValidator (with the regex
ValidationExpression="^([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 ValidationExpression, 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.com" and you click away, the text stays
"mm*@mmm.com".

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
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 ...
2
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
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...
12
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...
10
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...
2
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...
8
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...
5
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...
7
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...
10
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.