shaz
shaz

Reputation: 195

add server side validation using jQuery

I have added the client side validations on a form using jQuery's validate() function and assigned rules and it shows the error messages in Red color below the respected fields. I want to show same error messages after performing server side validations done in PHP. Please help.

Upvotes: 1

Views: 821

Answers (4)

David Robbins
David Robbins

Reputation: 10046

Is your goal to validate an individual field via a call to your server before you submit the entire form? Essentially you will need to perform a post ($.post) for each piece of data that you want to validate. This link is to the jQuery documentation for Ajax posts.

Upvotes: 0

o.k.w
o.k.w

Reputation: 25810

There's no automatic way (unless someone built one) that can replicate the client-side validation to server-side.

You might want to see how someone manage to come out with a working solution here:
jQuery for Real time Server-side Form Validation

Upvotes: 1

rahul
rahul

Reputation: 187070

You have to just issue a request to the server using jQuery. As far as validation on the server side jQuery has nothing to do with that. jQuery is an advaced javascript framework that runs inside the browser.

Upvotes: 1

Sampson
Sampson

Reputation: 268414

You'll have to perform that logic with PHP, in a sense replicating all that jQuery's validate() plugin does. I generally run through a series of tests, and add errors to an array:

$errors = array();
if (!valid_name($firstname)) $errors[] = "Please provide your first name.";
if (!valid_email($email))    $errors[] = "Please provide a valid email address.";

Then, after all of my rules, I'll decide whether or not to post the data or show the errors:

if (count($errors) > 0) {
  show_errors($errors);
  show_form();
} else {
  submit_data();
}

This is a format that has worked well for me.

Upvotes: 2

Related Questions