|
|
Subject:
Javascript - Validation Field Colour Change
Category: Computers > Programming Asked by: nyoung-ga List Price: $15.00 |
Posted:
11 Oct 2004 09:39 PDT
Expires: 10 Nov 2004 08:39 PST Question ID: 413180 |
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. |
|
Subject:
Re: Javascript - Validation Field Colour Change
Answered By: palitoy-ga on 11 Oct 2004 11:01 PDT Rated: |
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
rated this answer:
and gave an additional tip of:
$4.00
Absolutely fantastic, works like a dream. I now know my first stop for javascript queries. Thanks very much. NYoung |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |