Engram does not currently provide support for this item.

19 comments found.

Is the script able to validate certain domain extensions? i.e limit registrations to only users with .edu domains


Hello Daniel!

Thanks for your comment.

Yes, you are able to specify you own validation rules so therefore you are able to validate any input as you want. More specific to your question I would set up a custom rule for the input field containing the domain. The custom rule would then point to a function and this function would validate if the domain ended with .edu or not.

An example. Assuming you already have your Form Validation object, first you create the validation function, it might look like this:

function check_edu($value) { $rest = substr($value, -4); return $rest == ’.edu’; }

After that, simply add the field properties like this:

$form->add_field(‘domain’, array(‘required’ => true, ‘format’ => ‘alfanum’, ‘custom’ => ‘check_edu’, ‘label’ => ‘Domain’ ‘error_labels’ => array(‘custom’ => ‘The domain must end with .edu.’) ));

Your check_edu function might need to validate the domain url itself before checking the end part of it. Hope this helps!

Is it possible to make any one of 3 fields required? i.e. 3 telephone number fields of which at least one has to be completed?


Hello Jim,

Yes, this is possible. To achive this, what you need to do is set up your three fields but only set a custom validation rule on one.

    array('format' => 'alfanum', 
        'custom' => 'check_telephone', 
        'label' => 'Telephone', 
        'error_labels' => array('custom' => 'Please enter atleast one telephone number.')
        array('format' => 'alfanum'
        array('format' => 'alfanum'
The check_telephone function is the functions we call on submission. And this is just an example but I would something like this:
function check_telephone($value) {
    $telephone1 = $value;
    $telephone2 = $_POST['telephone2'];
    $telephone3 = $_POST['telephone3'];
        return true;
        return true;
        return true;            
    return false;    
In this validation rule, which is connected to the telephone1 field, we check if ANY of the three telephone fields contain any data. If so we return true which gives us a valid form, if not we see the error. Upon this question I noticed something in the validation that might be false sometimes and true other times. In the form_validation.php file, replace line 73 with:
When a form field is empty, you still want to run your custom validation but sometimes you might not want to run it. If your field is empty for example. Other times you want to run it if the field is empty. So, to get this example to work properly, you must replace line 73 since we can’t add a required validation rule on either of the three telephone fields.

Hope this helps. Otherwise I’m always available via email.

It works really good. Just a question, is there a way to add a red border on wrong field? Where do I have to put “style=color:red”?

Hello there totemx,

Thanks for the purchase! I’m really glad it’s working for you.

A simple approach for your problem would be to use the get_validation_errors() method available for the form_validation object. If you pass the name of the field you want to fetch errors for as an argument to this method, the method will return errors specific for this field. We can simply then count the number of errors and if the number is larger than 0 we put a red border around the field in question.

It might look something like this:

<?php $bg = count($form?>get_validation_errors('username')) ? 'style="border: 1px solid red;"' : ''; ?> <label for="username">Username</label><br /> <input type="text" name="username" value="<?php echo $form?>get_value('username'); ?>" <?php echo $bg; ?>/>

In this case I’m fetching errors for the “username” field. If we find errors we assign a variable, $bg, the style property value for adding a red border. It might be more professional to set a class instead of using inline css. But I think you will get the idea.

Hope this helps!

Thanks for the answer, really clear and exhaustive. Is there a way to avoid using the function for each field? Maybe creating a function and printing only such as “blablalba(‘username’)”. Jut only to make count and check 1 time. Thanks man

Well, you have to do it for each field, since each field is unique. It will not exhaust the server nor anything else since the validation is only run once and each error saved in a internal array.

To make the call a bit nicer you can add a new function to your form page call something like mark_as_error(), taking the form_validation object and field name as arguments. It might look something like this:

function mark_as_error(&$form, $field_name) { return $form->has_errors($field_name) ? 'style="border: 1px solid red;"' : ''; } And for each field you simple call the function: <?php $bg = mark_as_error($form, 'username'); ?>

So in short, you need to call the function for each unique field. Let me know if you need more support! I’m also available on my email.

Really interesting. Just the last question. How can i print single unique error? Example:

label – input + red colored error label – input + red colored error etc…

Thank you so much

With the get_validation_errors(field_name) method you get all unique errors for a single input field. You can loop through this array of errors and see if the one you are looking for is there. It is currently not possible to fetch a single error for a unique field, say like username – required and it returns true/false. Might be a good update though.

Finally a update it’s under review making it possible to fetch a single error for a specific field using:

$form->get_validation_errors($field_name, $rule_name);

Ok, I’ll wait for the update so, thank you

Hi. I’ve got two questions:

1. ZDE 5 marked code as incorrect in form_validation.php private static $regexp = array(...). I guess it is ok, but just wanted to make sure – http://www.onlinedisk.ru/get_image.php?id=699455

2. Apart from validation I still should use sanitazing of data, for instance filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); and not forgeting constructions like that: if (!$_POST['var']) { $var= ''; } else { $var = strip_tags(mysql_real_escape_string(trim($_POST['var']))); }

And here goes the second question. In the 3rd example (sampleproject_3.php) the form action=”” – the same script sends and processes data. And there’s no $_POST[‘somevar’]. So, am I right, that I should use filter_var($form->get_value('email'), FILTER_SANITIZE_EMAIL);

Hello Poxy,

To answer your questions: 1. I’m not sure why ZDE 5 marks the code as incorrect. I have tried it out in Aptana Studios and Coda and no errors can be found. I believe the code works as it should. Thank you for pointing out this issue though.

2. The script only handles validation rules and o sanitizing so yes, you should definitely sanitize the input. When the form is posted you will (or should be if everything is correct) have access to the $_POST variables and there you should find the fields posted, even if they have been used in the form validation or not. At the same time, you can also get the value by using the $form->get_value(‘field’) method if you want.

Let me know if there is anything else I can support you with. Thanks for purchase!

URL validation rule. I have made simple changes in form_validation.php

1. added the new regexpression, so that private static $regexp now looks like that:

private static $regexp = array( 'integer' => "/^[-]?[0-9,]+$/", 'alfanum' => "/^[0-9a-zA-Z ,.-_\s\?\!]+$/", 'word' => "/^[A-Za-z]+[A-Za-z \s]*$/", 'text' => "/^[\d\D]{1,}$/", 'url' =>"/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/" );

2. In the in method valid_format() line 225 I have added the line: case 'url': return (preg_match(Form_validation::$regexp[$format], $value)); break;

So now I can validate url:

$form->add_field(‘url’, array(‘required’ => true, ‘format’ => ‘url’));

how can you echo out values set in a database? for example if i was creating a multi-step form and wanted users to come back as any stage to edit or complete the form.


Hi John,

I think your question about echoing out values from a database is a bit out of the scope of the form validation script. The script is focused on form validation, not using any database connection.

If you are using multiple steps you are required to set up different form validation rules for each step otherwise you script will not validate. I will try to answer more in your email.

Hi, can I use this to create a form that supports conditional fields? Or prepopulated fields?

Where the choice made in field A, determines the choices available in Field B?



The script helps you to validate form input, you can set up your custom rules saying if Field A has the value B the Field C is required or must have the value of D. All this is available in your own custom functions which will be called upon form submission.

What you have to do yourself is to create the Javascript part (if any) to show/hide non-required fields or your conditional fields.

Hi efeflash,

I recently released a new script called “Master Form Builder” with the options you asked for here. Take a look at it by searching for “Master Form Builder”.

I must say when I bought this script some time ago (was one of the first scripts bought from here) I did not understand anything about this script. Today I finally found again this script and I am very happy I have this, makes my life so much easier!

Thank you!

Hello! Thank you very much for your kind words and for your time coming back and posting this comment. I’m glad you are able to use it!

Hello, does this work with input names that are part of an array? Example:

$form = new Form_validation('_index'); $form->add_field('input[full_name]', array('required' => true, 'format' => 'text', 'label' => 'Full name'));

is_valid never seems to equal true for me in this experiment.


Thanks for your question. Sadly array names doesn’t seem to work. It is only valid for checkboxes. Currently I’m unable to change this.

Also, one additional question. I see l can use $form->get_value('firstname'); To get the value of a form field, is there a way to set, or override this? For example: $form->set_value('firstname') A use case example of this is that we prepopulate our form field(s) with info from a database (before the form has been submitted), and l’d like to use this.

Hello again brainhost,

After looking through the code in more detail you can simple supply a “value” key in the second options argument when adding your fields to the form with a default value such as:

$form->add_field('username', array(
    'required' => true, 
    'format' => 'alfanum', 
    'custom' => 'check_login', 
    'label' => 'Username',
    'value' => 'My default value',
    'error_labels' => array('custom' => 'Your username and password did not match.')

Hope it helps.

I’m sorry but this doesn’t seem to work.

Hmm that’s weird. It’s working for me. Are you adding a “value” key word with your default value into your add_field second argument? Does the value come up empty or do you get an error message?

Considering purchasing. Can this validator work in the following scenario: the user will create an account, I want it to be able to check if the email address has been used in the db before. If so, then a warning shows… if not, then a green check / username free type text pops up.

Thanks!!! Hope this works!


You are able to add your own custom validation rules which will be handled upon a form submit. You have to write the email address validation function yourself and then add it to the form fields.

From the previous answer, here is a snippet of code:

$form->add_field(‘email’, array( ‘required’ => true, ‘format’ => ‘alfanum’, ‘custom’ => ‘myEmailValidationFunction’, ‘label’ => ‘Email’, ‘error_labels’ => array(‘custom’ => ‘Your email has already been used.’) ));

would validate the attach a file in the form. What would the code?


It can validate a file in form by a custom function or by the included file validate function found in the documentation. Basically you include the “callback” keyword in the “data” field and can validate the file based on your own criteria. I recommend to browse the documentation for this question.

Thank you for your comment and time.

thank you very much I could solve

the value “value =” <? php echo $ form-> “as would apply in a text field?

<textarea tabindex = “29” name = “comments” cols = “15” rows = “5” class = “camp_comments” id = “comments” value = ”<? php echo $ form-> set_value (‘comments’); ?> ”> </ textarea>


In the textarea you need to echo the set_value function inside thte < textarea > tags: < textarea> <? php echo $ form->setvalue(‘comments’); ?> </ textarea >

Hope it helps!

Thanks, I served very well.

I came another question.

I have a checkbox

<input type = “checkbox” name = “2” id = “2” <? php echo $ form-> is_checked (‘accounting’, ‘2 ‘);?> value = “2” /> Accounting

and sent this:

$ Message_body. = “Question”. $ _POST [“1”]. “

But when I edit this:

<input type = “checkbox” name = “accounting []” id = “3” <? php echo $ form-> is_checked (‘accounting’, ‘accounting’);?> value = “accounting” /> Accounting

Do not send

Sending so I changed it, but neither.

$ Message_body. = “Question”. $ _POST [Accounting]. “

Hmm, currently I’m unable to run the scripts myself but I believe you should name your checkbox “accounting” and if you set the value to “accounting” as well you should be able to use the is_checked(“accounting”, “accounting”). The first parameter should be the name of the checkbox field and the second the value you want to check.

In your first try the name of the checkbox is “2” and the value was “2” so to get the is_checked function to work propertly you should use: is_checked(“2”, “2”)

Hope it helps mate.


Can it also be used for a feedback form?

Hello ryan,

The final form can be used as any other “standard/normal” form. This script will help you validate a form, how you decide to use it is completely up to you.

Regards, Niklas

i used it to insert to mysql but it isn’t work

Maybe I misunderstood your question. What kind of update are you looking for in regards to MySQL?

at the end of form we include a hidden input right? then i put this

<input type="hidden" name="_foo" value="formid" />

When i try remove

it worked, the validate catch but no row insert :( :( :(

my form must have

. it direct to bla bla… can not be null.

what if i want more than one custom errors ?


Thanks for your comment.

Currently the script only supports one call to a custom function. Although, this function can easily call multiple function in it self. Using the custom function attribute you can add as many custom errors as you want.