On a HTML page I have an inputbox that has a 'watermark' on it when it is empty. (eg: "enter text here..."). Kind of like this: http://digitalbush.com/projects/watermark-input-plugin/ - but custom written.
The problem is that I can't figure out how to validate this field with the jQuery validation plugin (http://docs.jquery.com/Plugins/Validation/) so that it treats my watermark text as if the field was empty.
I can't find an option in the jQuery validator to let me specify a custom rule for when a field is valid, is there one? I could find options that allow me to specify whether a field needs to be validated based on custom logic, but not how it should be validated.
What am I missing?
When using unique watermark labels for each of your testboxes (For example 'enter firstname', 'enter last name'...), you could improve the script to:
This also removes the hardcoded text from your script.
Not sure how the validation plugin works, but this is a separate module that is usable.
Check out this blog post:
http://randomactsofcoding.blogspot.com/2008/10/starting-with-jquery-how-to-write.html
Tells you how to construct a custom validation rule for a field.
Thanks to Kazar for providing me with the link, I came up with the following solution (if anyone is interested):
I'm using the Watermark plugin for jQuery, but my situation was similar:
http://code.google.com/p/jquery-watermark/
It uses a classname for the display of the watermark. (I'm not sure if the DigitalBrush version uses a class or not.) I've modified the above function to use jQuery's hasClass() function to determine whether or not the field is evaluated as "empty" based on currently assigned classes.