File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Dynamically changing an inputText field's background color based on user input 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 "Dynamically changing an inputText field Watch "Dynamically changing an inputText field New topic
Author

Dynamically changing an inputText field's background color based on user input

Rob Phair
Greenhorn

Joined: May 01, 2008
Posts: 3
Hello, all,

The project that I am currently working on includes a requirement to validate an input field on the fly, as the user is typing. The idea is as follows:

- When the user begins typing in the field, the background color should change from the default white to yellow.
- When the user has typed a valid sequence of characters, the field should change back to white.
- If the user continues to enter further input, and that invalidates the string, the field should change back to yellow.

The project is being built with JSP, and I am using the <h:inputText> tag for my text box. As I'm very new to JSP, I am wondering if there is a simple solution involving an ActionListener or EventListener of some kind that I can use to implement this functionality. Has anyone here ever put something together like this?

I appreciate any help you can offer, including even a simple "there's no way you will get that to work." I just need to know if it's possible or not. Thanks in advance!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The behavior you're describing needs to be written in the client code, (HTML/Javascript) not the server side (JSP/Struts).

I'm going to move this to our HTML/Javascript forum for you.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Originally posted by Rob Phair:
The project is being built with JSP, and I am using the <h:inputText> tag for my text box.

<h:whatever> is not any standard JSP tag. Is this perhaps a JSF project? If so, it's important to state that.

As Ben pointed out, that something that needs to be done on the client, not the server.
If we were to ignore whatever snags that the use of JSF throws into the mix, this is a simple matter of placing a few JavaScript event listeners on the field and manipulating its CSS based upon its current value.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
I'm just wondering, may be you can use the onkeydown event attribute of the textbox to invoke a JavaScript method, which can run the set of validations and set the textbox's backgorund colour accordingly.
As Bear says, <h:inputText/> is not a standard tag, so you'll have to see that your <h:inputText/> tag does support the "onkeydown" event attribute. If it does, then perhaps you can validate the text at the client side itself instead of engaging the server.
Vilmantas Baranauskas
Ranch Hand

Joined: Dec 20, 2006
Posts: 89
Can you perform validation of value on the client-side? If yes, simple JavaScript would be sufficient.

If no, you may still implement validation using AJAX.


Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
 
jQuery in Action, 2nd edition
 
subject: Dynamically changing an inputText field's background color based on user input
 
Similar Threads
Changing the background color on JList's
programmatic change of text item color in JLIst
setting background color
? JTextField disabled bg color sometimes grey & sometimes white on Windows Lk/Feel !?!?
need JTextField that change color when focused