Vish
Vish

Reputation: 4492

How to validate Forms in PHP

Is it better to validate PHP forms using php and then redirecting with errors, or is it better to use javascript validation and then just allow form submission if javascript is enabled.

Upvotes: 4

Views: 333

Answers (5)

Capitaine
Capitaine

Reputation: 2005

use javascript to validate format, e.g. email format, password length, etc.

use php to validate db if same record exists, e.g. same username

except hackers, 99% ppl enable javascript, so just throw this worry away.

here I recommend jquery validation plugin for client side validation.

Upvotes: 1

tyler.dunn
tyler.dunn

Reputation: 31

Definitely both. Users can turn off Javascript or even edit your validation script since it is local (using Firebug or some other such tool). Yes, this means that you will likely write the same validation script in two separate languages at approximately the same time, but that's an essential way to avoid incorrect or malformed data from being submitted.

Upvotes: 1

Achilleterzo
Achilleterzo

Reputation: 742

Is better making both together because php can handle raw request without using or enabling javascript, javascript, can speed-up the check process and handle it in an elegant graphic way

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

You must validate values on the server side, since the client cannot be trusted.

You may validate values on the client side to provide better UX for those with JavaScript enabled.

Upvotes: 11

Tesserex
Tesserex

Reputation: 17314

I usually do both. If you can check the fields in javascript, it saves them the loading time and spares your server the hit. You still have to check again on the server in case they had javascript disabled, or intentionally bypassed it.

Upvotes: 4

Related Questions