[SOLVED]Custom validation rule-compairing validation-help

Attros 14 Dec, 2009
Dear Support!
I'm using CF from a while and I'm satisfied with the product.
But I need to use a custom validation rule and I'm not a programmer so if it's possible please support me to resolve this issue.
For instance, if the user enter a predefined unique registration number in the registration form, it must only allow a number which is on a predefined list. It will not allow a number which is not known. These codes will not be visible to the public (by ex. in a hidden field).It's something similar to the password validation script what it use the CF except that the script must to check if the unique reg. number exist or not in the predefined list. If exist then the reg. process is allowed and if not exist then display an error message.
While I tried to find a script for this on the net I find something similar on RSForm's site. Here is the code from there:
if(isset($_POST['form']['RegistrationCode']))
{
$validCodes = array('AAAA','BBBB','CCCC','DDDD');
$foundValid = false;
foreach($validCodes as $validCode)
if( strtolower($validCode) == strtolower($_POST['form']['RegistrationCode'])) $foundValid = true;

if(!$foundValid)
{
$invalid[]=RSresolveComponentName('RegistrationCode',$formId);
echo RSshowForm($formId, $_POST['form'], $invalid);
}
}

Note:
Instead of "RegistrationCode" you have to put in the name of the textbox form component that will hold the registration code filled in by the user. 

Well I can not make this script to function on my site.
I searched a lot in this forum but I not find any topic related to this. Maybe I miss something so please help me to resolve this issue.
Kindly,
Attila
GreyHead 14 Dec, 2009
Hi Attila,

You could use something very much like the RSForms code in the server-side validation box on the Validation tab:
<?php
$code = JRequest::getString('registration_code', '', 'post');
$code = strtolower($code);
$valid_codes = array('aaaa', 'bbbb', 'cccc', 'dddd');
if ( !in_array( $code, $valid_codes) ) { 
    return "You must enter a valid code";
}
?>
Where again 'registration_code' needs to be replaced by the name of the code input in your form.

This will work OK if the list of valid fields is fairly short and doesn't change much. If there's a long list or the list changes then you'd be better off putting the codes into a database table and using a database query to check them.

It would also be quite possible to use JavaScript to set up an Ajax check on the code directly from the form without waiting for the form to be submitted to check.

Bob
Attros 14 Dec, 2009
Dear GreyHead,
you are lightening fast!!! Thanks, its working 100%!
In other hand are you right.
It's a long list (now a couple of hundreds)but it will be thousand's and they are not changing only are added new reg. numbers (the list is growing)not to frequently.
Your's advices are right but I have only beginner skills on PHP and only basics on Java(Ajax), so I can not make such a solution. I can manage the SQL but not the interface for this. I'm developing only Joomla CMS's, production sites in html, graphics, etc.
If you could provide me at least the Ajax solution I would be grateful.
With such support I think that I will subscribe to the paid version.
Thank you, you are great!
Respectfully,
Attila
GreyHead 22 Dec, 2009
Hi Attros,

I can't write the code for you right now but there's an example of an AJAX set up in this post that you could adapt.

Bob
This topic is locked and no more replies can be posted.