Support Forum Softcomplex logo
About Us  | Services  | Download  | Order  | Support  | |
| Search | Today's Posts | Stats | Board Rules | Not logged in [ Login | Register ]
go to bottom
Last active: Never

Printable Version | Subscribe | Add to Favourites   Post new thread
Author: Subject: conditional validation rules (dependency)
ewel
Junior Member

Posts: 3
Registered: 1/29/2006
Member Is Offline
posted on 2/23/2006 at 11:41 PM Reply With Quote
conditional validation rules (dependency)


In search for a way to make some fields required dependent on some condition, I came across some posts in the forum about dependency, and discovered that there was no cut&paste code available. So, I got to work on some code myself.
Since there seem to be others who are eager to use conditional validation rules, I thought it would be good to post my script.

The main body of my script is as follows:
Code:

var setTestdefault;
function changeValidation(testFields,Iswhat,fieldCondition,toFields,inTest,Testfor,Testdefault) {
for (var i = 0; i < testFields.length; i++) {
if (
(Iswhat == 'is' && testFields.value == fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'isnot' && testFields.value != fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'more' && testFields.value > fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'less' && testFields.value < fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'equal' && testFields.value.length == fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'notequal' && testFields.value.length != fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'greater' && testFields.value.length > fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'smaller' && testFields.value.length < fieldCondition && testFields.value.length > 0) ||
(Iswhat == 'empty' && (testFields.value == fieldCondition || testFields.value.length < 1)) ||
(Iswhat == 'notempty' && (testFields.value != fieldCondition || testFields.value.length > 0)) ) {
setTestdefault = false;
for (var i = 0; i < toFields.length; i++) {
a_fields[toFields][inTest] = Testfor;
if (Iswhat == 'empty' || Iswhat == 'notempty') {
a_fields[toFields]['c'] = 0;
}
}
}
else { if (setTestdefault != undefined) { setTestdefault = true; } }
}
if (setTestdefault == true) {
for (var i = 0; i < toFields.length; i++) {
a_fields[toFields][inTest] = Testdefault;
if (Iswhat == 'empty' || Iswhat == 'notempty') {
a_fields[toFields]['c'] = 1;
}
}
}
}


To keep the form document light, this can be inserted in the javascript source file.
No doubt there are ways to make this code more efficient, but I am not a programmer so this is the best I can do.

It would have been slightly more simple if I had not wanted to test for empty or non-empty fields, and set required to true or false dependent on that. This was apparently complicated by the fact that for some unrelated reasons, all fields in my form are set to an initial value of "". I still don't know exactly what the issue was, but to get that solved, I had to add an extra validation rule 'c', which serves no other purpose than to set a flag I needed. If you do not have the same issue, you can delete the lines of relevance to ['c'], which are lines 18, 19, 20, 28, 29 and 30.

The test for this flag is && !this.a_fields[n_key]['c']. You would need this flag too if you have any fields set to an initial hard-coded value of "", but otherwise you can ignore this part. This needs to be inserted in the main Validator code, where it checks for required fields, as follows:
Code:

// check reqired fields
if (this.a_fields[n_key]['r'] && !this.a_fields[n_key]['v'] && !this.a_fields[n_key]['c']) {
this.a_fields[n_key].n_error = 1;
n_errors_count++;
}


To initiate the changeValidation function, one or more other functions are needed. For these I made the following template. The things you need to fill in are put between <brackets>.
Code:

function validateDependency<number> (){
var inForm = document.forms[0];
var testFieldnames = new Array(inForm.<field name>);
var toFieldnames = new Array('<field name>');
changeValidation(testFieldnames,'is',<condition>,toFieldnames,'<validation rule key>','<new validation rule value>','<original validation rule value>');
}


So, by way of example, if you take Tigra's form validator demo 1, and you set the validation rule values of company and company_number to false, then the following function would change the validation rule key 'r' of both fields to true if either field is filled in, and back to false if neither field is filled in.
Code:

function validateDependency2 (){
var inForm = document.forms[0];
var testFieldnames = new Array(inForm.company,inForm.company_number);
var toFieldnames = new Array('company','company_number');
changeValidation(testFieldnames,'notempty','',toFieldnames,'r','true','false');
}


Finally, to set of this validateDependency function, you need to add an onBlur event to the relevant fields. In the example that means adding onBlur="validateDependency2();" to both fields. The row in Tigra's demo 1 would then be like this:
Code:

<td colspan="2" id="t_company">&nbsp;Company:</td>
<td colspan="2" valign=top><input type="text" name="company" value="" size="12" class="ctrl" onBlur="validateDependency2();"></td>
<td id="t_company_number">&nbsp;Company&nbsp;Number:</td>
<td colspan="2"><input type="text" name="company_number" value="" class="ctrl" size="10" onBlur="validateDependency2();"></td>


As you can see from the code, you can apply a numer of different tests.
Iswhat must be either 'is', 'isnot', 'more' or 'less', respectively, if the test field value should be equal to, not equal to, greater than, or less than the condition, or 'equal', 'notequal', 'greater' or 'smaller' if the test field value length (length!) should be equal to, not equal to, greater than, or less than the condition, or 'empty' or 'notempty' if the test field value length should be equal to or not equal to zero (in which case the condition can be left empty).
Variables testFieldnames and toFieldnames can be arrays of fields or single fields, and can overlap or not.

I cannot guarantee that this will work for you, but for me it does everything I needed. If you want to see for yourself if this works, you can download the example file.

Attachment: demo1_dependencies.zip (9.4kb)
This file has been downloaded 1107 times
View User's Profile View All Posts By User U2U Member
bali2002
Junior Member

Posts: 3
Registered: 4/11/2007
Member Is Offline
posted on 4/12/2007 at 02:35 PM Reply With Quote

It doesn't seem to work with the tigra form validation PRO :(
View User's Profile View All Posts By User U2U Member
Post new thread

Related Links:
Product Page Tigra Form Validator
Product Demonstrations Product Demos
Registration Form Validation
Login Form Validation
Free Download Free Download
Product Documentation Documentation
Documenation Page
Change Log
Validators Comparison Table
Support Forum Support Forum
 
Tigra Form Validator PRO
Product Demonstrations Product Demos
Registration Form Validation
Login Form Validation
Multiple Forms Validation
Product Documentation Documentation
Documenation Page
Change Log
Validators Comparison Table
Support Forum Technical Support
Support Forum
Customer Care System
Order Now ORDER NOW!
Add to Cart
View Cart
 

Go To Top


Powered by XMB 1.9.1 Nexus
Developed By Aventure Media & The XMB Group © 2002-2004