locked
PHP - change class on error RRS feed

  • Question

  • Hi

    I have a php form on a epression web site which is validated at the moment with text strings (sorry don't know the correct phrases for this).  If you press submit on the link you see what I mean.

    I would like, instead of the text strings to simply change the class of the form controls if they are empty - on submit - without deleting the text already inserted.

    I have 2 classes
    form_cell_background and form_cell_background_red.  Default is the 1st and I would like if OnSubmit to change a form control's class to the 2nd.

    This is a trail copy of the form
    http://www.ukoutdoorpursuits.org/document_copy(2).php

    Is this possible "without" deleting the items already completed.  eg.  If a user types in some text into a control and presses Submit the class is changed on the empty controls without deleting the text already input.

    Thank you
    (sorry for my english not being good)

     

     

    Saturday, May 15, 2010 10:41 AM

Answers

  • It is probably easiest to do this with JavaScript or jQuery. You could do it by calling a function using the onmouseout attribute of the particular input tag. The function would examine the field contents and, if blank, turn the background red.

    But I don't recommend the approach. It will result in a form that makes fields red even if all the user wants to do is tab through to fill in another field, intending to come back and complete the missed field later. I think it is better to validate on form submision and check everything at once.

    I use a validation tool from Michael Leigeber called Leig-Validate. You can see it in action at my own contact form - just click on Submit without entering anything. I like the way it works but you will need a different version of the code for each form to which you apply it if those forms have different fields requiring validation.


    Will
    Baltimore, MD USA - www.fastie.com
    Tuesday, May 18, 2010 3:31 PM