omar
omar

Reputation: 1611

Is there a design pattern in the building of website validation with JS and PHP?

For example, I want avoid errors if someone disables Javascript... I think my JS validation must be called by my PHP code...

I'd like know about design patterns or anti-design patterns.

I should clarify that I must use JS in my project and not tools like jQuery or Dojo

Upvotes: 3

Views: 482

Answers (2)

GSto
GSto

Reputation: 42350

There are several jQuery plugins that have validation, and some tie it to classes on the fields. This way, you can keep your javascript and your PHP separate.

The important thing is that there needs to be two sides to validation: server-side and client-side. So while you can validate with JavaScript, mainly for usability reasons, you need validation on the server side as well, for reliability reasons. This way if someone has turned validation off, you can still prevent errors that un-validated data may cause.

Upvotes: 1

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

The problem is more often how to keep both validation identical on the client using JS and on the server using PHP. What i often do is i create my validation on the server only using PHP and i send the content through AJAX using jquery.

The server sees that request as an ajax request because i pass it a content-type and then it doesn't process anything, just validates and returns a JSON response saying everything is ok, or it sends back the error messages that jquery will have to show to the user.

Like you asked, it's only a design pattern, a way of doing. Implementing it is another story, there are tons of ways to do it.

Good luck

Upvotes: 1

Related Questions