Aug 21st, 2010, 8:52 AM
Hi Tim,
Ah, tricky one! First thing I'd suggest is checking out some plugins that would help you with this, like the jQuery coolinput plugin. Plugins like that often let you configure it to not send the values on form submission. They may well have something for checking validation as well - I'd be very surprised if they don't have something in place.
But if you're doing it manually (e.g. just providing value="default value" attributes), you'd need to hack it a bit.
Here's an example. Say you have a couple of text fields for first and last name, with the default values "Enter first name" and "Enter last name". You'd need to change your validation rules from this:
to this:
Then, add a hidden field to your form:
It's inelegant, but it would work. What the new rules do is check to see if the first_name and last_name fields had the default value, then continued on to check if the blank_field was empty (which it always will be). Therefore, it will always generate the error whenever the fields have the default value.
Kludgy? You betcha. So I'd definitely look into a separate plugin/script to provide the default values of your form - and one that was compatible with running validation tests on the affected fields.
Good luck!
- Ben
Ah, tricky one! First thing I'd suggest is checking out some plugins that would help you with this, like the jQuery coolinput plugin. Plugins like that often let you configure it to not send the values on form submission. They may well have something for checking validation as well - I'd be very surprised if they don't have something in place.
But if you're doing it manually (e.g. just providing value="default value" attributes), you'd need to hack it a bit.
Here's an example. Say you have a couple of text fields for first and last name, with the default values "Enter first name" and "Enter last name". You'd need to change your validation rules from this:
Code:
rules.push("required,first_name,Enter your first name.");
rules.push("required,last_name,Enter your last name.");
to this:
Code:
rules.push("if:first_name=Enter first name,required,blank_field,Enter your first name.");
rules.push("if:last_name=Enter last name,required,blank_field,Enter your last name.");
Then, add a hidden field to your form:
Code:
<input type="hidden" name="blank_field" value="" />
It's inelegant, but it would work. What the new rules do is check to see if the first_name and last_name fields had the default value, then continued on to check if the blank_field was empty (which it always will be). Therefore, it will always generate the error whenever the fields have the default value.
Kludgy? You betcha. So I'd definitely look into a separate plugin/script to provide the default values of your form - and one that was compatible with running validation tests on the affected fields.
Good luck!
- Ben