Drop Shadow

Custom form verification

If your form has required fields, but you don’t know which fields are required until checking the data in other fields, you can’t just rely on a single list of required fields. For example, if you have a pull-down list of employee or student, you might need to check the value of that pull-down list before you know which fields are required.

First, implement the logic in PHP. You’ll need to do that anyway, and this will give you a better idea of how the JavaScript will flow. Always remember that JavaScript is not security! You can only be sure of requirements if you perform them on the server, and JavaScript is performed on the client, if it is performed at all; it is under the complete control of the browser.

Override the default form verifier

If you want to implement the special code in JavaScript as well as server-side, you’ll need to override the default form verifier. If you are using forms.phpi, use the “verifier” method:

$contactForm->verifier("myVerify");

In this case, instead of calling the default JavaScript function, it will call the function “myVerify”. You must create that function. It should accept one parameter, which is the form that was submitted. It should return “true” if the form is valid, and “false” if it is not.

Getting field values

You can use the function “fieldValue” to get the value of a field. Give it the form and the name of the field, for example, “fieldValue(theForm, "Department")”.

Verifying a list of fields

You can use the function “verifyFields” to verify that a field or list of fields contain something. This function assumes that any field in the given list must contain something; if you’ve specified that one or more of the fields must be numeric or match some other criteria, it will also verify that as well. It does not verify any fields except the ones you give it.

The verifyFields function returns a message that can be displayed via “alert” if any of the fields do not verify.

<script language="JavaScript"> function myVerify(theForm) { var status = fieldValue(theForm, "status"); var requiredFields; if (status == 'employee') { requiredFields = new Array('department', 'hire date'); } else { requiredFields = new Array('major', 'graduation year'); } if (messages = verifyFields(theForm, requiredFields)) { alert(messages); return false; } else { return true; } } </script>

If the status form field is “employee”, the function sets an array containing department and hire date. Otherwise, the function sets an array containing major and graduation year. It then calls verifyFields with that array. If any messages come back, it displays the message and returns false, blocking form submission.

Otherwise, it returns true and lets form submission continue.

Pass to the normal verify function

If you want to run the normal verification script, use “verifyForm”. For example:

<script language="JavaScript"> function myVerify(theForm) { var status = fieldValue(theForm, "status"); var requiredFields; if (status == 'employee') { requiredFields = new Array('department', 'hire date'); } else { requiredFields = new Array('major', 'graduation year'); } if (messages = verifyFields(theForm, requiredFields)) { alert(messages); return false; } return verifyForm(theForm); } </script>

That runs the normal verification after running the custom verification. If any error messages come back from the verifyFields function, the custom myVerify returns false, and the form is not submitted. If there are no error messages from the custom verification, the custom myVerify returns whatever the standard verifyForm returns.