This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to color a blank textfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to color a blank textfield" Watch "How to color a blank textfield" New topic
Author

How to color a blank textfield

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
I am using some JSP but I want a textfield colored red if it is blank. If this can be done in Javascript, please let me know. Any help is greatly appreciated.

Thanks.

-------code-----------
if the textfield has no value, color it red. if user inputs a value then take color out.

<td><input name="textfield1" type="text" size="30" value= "<%=N_ASDR1 %>"></td>
<td><input name="textfield2" type="text" size="30" value= "<%=N_ASDR2 %>"></td>
<td><input name="textfield3" type="text" size="30" value= "<%=N_ASDR3 %>"></td>
<td><input name="textfield4" type="text" size="30" value= "<%=N_ASDR4 %>"></td>
----------------------
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
{bear edit: removed example too full of errors to be useful. Niki feel free to re-post once you fix the problems]
[ October 03, 2005: Message edited by: Bear Bibeault ]

Life called,so here I am.<br />Cheers<br />Niki.:-)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

You can color the background with the background-color CSS property.

For changing based upon whether the field is empty or not, you will need to establish event handlers on the text field to detect changes. The onchange handler itself will not be enough because it's not called on individual keystrokes. You'll need to also at least listen to onkeyup.

In the handler you can change the color of the background using CSS under Javascript control.

It's not foolproof however. I'm not sure how you can capture the field being emptied by a cut operation via the menu for example.

[ October 03, 2005: Message edited by: Bear Bibeault ]
[ October 03, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to color a blank textfield
 
Similar Threads
syntax for textarea
need help on debugging jsp
Getting "not a multipart request" even though the image is being saved.
javascript error: object expected
request.parameter