• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to color a blank textfield

 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic