The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check Init field value on validation
Hi Ben/All

I am sure I am missing something really obvious but here is my problem.

I like to have a set of initial values in my form so that people filling in the form can't go wrong. This is especially important on long lists on several columns of the same thing. However the JS validator see the example data as correct data.

I have a three column form for prescriptions the headings are:

Item name, Item strength, Item quantity

Only the first row are required fields but there are 11 more rows for large orders.

If I put example data in the first row then the JS validator sees the fields as completed and allows submission. Is there a simple way to check if the field matches the example data and if so throw a validation error.

I could write my own validation script but I'd like to use the bundled JS validator.

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:

rules.push("required,first_name,Enter your first name.");
rules.push("required,last_name,Enter your last name.");

to this:

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:
<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
Hi Ben

Great suggestion, early tests show a combination of the coolinput plugin and the php server-side validation do a great job.

Thank you very much for your help

Forum Jump:

Users browsing this thread: 1 Guest(s)