Javascript - Validation Field Colour Change
-
I currently have javascript validation set up on my form, (client side validation) it checks to ensure certain fields are filled in. I am looking for code that will also change the colour of the fields the script already validates if they are not compeleted. It will work with the validation script I have already, so coupled with the alert message that tells customers what fields they have missed I also want those fields to turn red say.
-
Answer:
Hello nyoung-ga Without seeing the current coding of your page I will have to try to give you a generic answer, should you require any further help at integrating it into your site please ask for clarification and I will do my best to respond promptly. The easiest way to tackle this problem is to use style sheets. In the example below I have used the onFocus and onChange events to show you how to alter the color of a form element: <style>.changecolor {background: blue}.currentcolor {background:red}</style> <form> First Name: <input type="text" size="10" name="myname" onFocus="this.className='currentcolor'" onChange="this.className='changecolor'"><br> </form> Therefore in your script if the field is not filled in you simply need to change the color of the form elements using your existing "if the box is empty" statement. An example of this can be seen below: <style>.errorcolor {background: red}</style> <SCRIPT LANGUAGE="JavaScript"> <!-- function valid(form) { var field = form.myname; if (field.value=='') { alert("You must indicate your name."); field.className='errorcolor'; return false; } return true; } // --> </SCRIPT> <FORM METHOD="POST" onSubmit="return valid(this)"> Your name:<BR><INPUT TYPE="text" NAME="myname" /> <INPUT TYPE="submit" VALUE="Submit" /> </FORM> Once again, if you require any further help on this please ask for clarification and I will do my best to help quickly. If you do require further help it would be very helpful to see your current page code and javascript script. For further information there is an excellent article here that discusses something similar to what you are trying to achieve: http://www.xs4all.nl/~sbpoley/webmatters/formval.html
nyoung-ga at Google Answers Visit the source
Related Q & A:
- How to get the Field Name,Field Length and Field Type?Best solution by Stack Overflow
- Is there email validation baked in to Rails 3?Best solution by coverhound.com
- How to change javascript values to php?Best solution by stackoverflow.com
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- How to change the colour msn name?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.