0

I have a form (firstname, lastname etc) which is validated client side using http://bassistance.de/jquery-plugins/jquery-plugin-validation/ validator. I am then posting the form fields to the server using ajax serialize() however I was wondering if someone could share a class (or link to class) for the server side validation.

I have tried using http://www.html-form-guide.com/php-form/php-form-validation.html but this looks like this class is expecting the name of the form field to apply validation.

Thanks

puks1978
  • 3,667
  • 11
  • 44
  • 103
  • why not checking your values manually? Seems easy enough without knowing your project... `empty()`, `is_numeric()` and `count()` is an easy start – Dominik Apr 03 '13 at 03:47
  • Hi I was going to do that if there wasn't a class readily available. There isn't a huge amount to form fields to check - is a number, not null, email etc. I just need to make sure the data sent is what is expected for the DB field. – puks1978 Apr 03 '13 at 03:54
  • In the time looking for something you'd probably finish the manual check twice... but I get it. I, too, get lost in details sometimes ;) good luck though – Dominik Apr 03 '13 at 03:59

2 Answers2

0

That library does not require the name of the form. If you look at the example that they have given you, they only use the names of the input fields.

<?PHP
require_once "formvalidator.php";
$show_form=true;
if(isset($_POST['Submit']))
{
    $validator = new FormValidator();
    $validator->addValidation("Name","req","Please fill in Name");
    $validator->addValidation("Email","email",
"The input for Email should be a valid email value");
    $validator->addValidation("Email","req","Please fill in Email");
    if($validator->ValidateForm())
    {
        echo "<h2>Validation Success!</h2>";
        $show_form=false;
    }
    else
    {
        echo "<B>Validation Errors:</B>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
          echo "<p>$inpname : $inp_err</p>\n";
        }
    }
}

if(true == $show_form)
{
?>
<form name='test' method='POST' action='' accept-charset='UTF-8'>
Name: <input type='text' name='Name' size='20'>
Email: <input type='text' name='Email' size='20'>
<input type='submit' name='Submit' value='Submit'>
</form>
<?PHP
}//true == $show_form
?>

The name of the form is "test". It is not being checked. Please post your source code so that someone can help you find what the issue is.

You can also checkout the following post for more libraries - Easiest Form validation library for PHP?

Community
  • 1
  • 1
A23
  • 1,596
  • 2
  • 15
  • 31
  • Correct however I am unsure how to use the form field names when I am posting the information using ajax. $_POST['firstname'] gets the value of the form field firstname. – puks1978 Apr 03 '13 at 03:55
  • If your input name is 'firstname', change the validator initialisation as $validator->addValidation("firstname","req","Please fill in a Name"); – A23 Apr 03 '13 at 04:07
0

...ok, if you are looking for sanitizing the passed value to make sure no bad things get through than you can look here: http://php.net/manual/en/filter.filters.sanitize.php

if you are looking for 'validation' based on some pre-set rules, please specify.

You can also partially sterilize the passed values like this if you do need only basic char. set:

function sterilize($input) {

$input = htmlentities($input);

$input = strip_tags($input);

    return $input;

}

FYI as you mentioned below, for the email address you could youse something like this:

$email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_SANITIZE_EMAIL): '';
Milan
  • 3,209
  • 1
  • 35
  • 46
  • Sorry, I am looking to validate the information i.e. email is a valid email address, firstname is not a null value etc – puks1978 Apr 03 '13 at 03:56
  • The solution here seems to work. In the post there is a link to http://www.phpro.org/classes/Validation-Class.html which seems to accept posted fields. Thanks :) – puks1978 Apr 03 '13 at 04:03