Caution: I'm no JavaScript expert so there may be errors in this post. If you find any let me know and I'll fix them.
ChronoForms makes it easy to include validation JavaScript with your form but there are some problems in using JavaScript from other sources or existing forms.
Mostly there are two problems:
- ChronoForms changes the form name and the JavaScript doesn't recognise it.
- How to call the JavaScript validation - what code to put where.
The form name
ChronoForms changes the form name you use in the Form Manager. If you enter 'my_form' the ChronoForms form will be called 'ChronoContact_my_form'. This is probably the biggest cause of JavaScript validation problems.
Validation scripts usually start with a line like 'function checkForm()' or 'function checkForm(form)' and these two handle the form name in different ways.
Style 1: 'function checkForm()'
The form is usually addressed with the code 'document.{form_name}' e.g. 'document.my_form'
To use this style with ChronoForms you would need to find and replace each 'document.my_form' with 'document.ChronoContact_my_form'. This is tedious and has to be repeated if you want to use the same validation with another form.
Instead I suggest that you change your JavaScript to the second style of function.
Style 2: 'function checkForm(form)'
This style of function uses the JavaScript variable 'form' to identify the form and the actual form name is passed to the function when it is called.
If your JavaScript has the other style, then change the function header to the 'checkForm(form)' style and replace every occurrence of 'document.{form_name}' with 'form' so that 'document.my_form.some_code' becomes 'form.some_code' etc.
Calling the JavaScript
Once you have edited the JavaScript to use the 'checkForm(form)' you can call it simply by adding this code into the ChronoForms 'Form tag attachment' field:
- Code: Select all
onSubmit="return checkForm(this)"
The 'return' means that the form will only be submitted if the JavaScript validation returns 'true' - this is how validation scripts are usually written so is unlikely to be a problem.
Here is a very simple example of a validation script written like this, it simply validates that the form field 'name' is not empty :
- Code: Select all
function checkForm(form)
{
if(form.name.value == ""«») {
alert("Please enter your name!"«»);
return false;
}
return true;
}
Bob<br><br>Post edited by: GreyHead, at: 2007/07/20 11:34
