简体   繁体   中英

PHP form validation from jQuery ajax post

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

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?

...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): '';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM