admin管理员组

文章数量:1292834

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.

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.

Share Improve this question asked Apr 5, 2011 at 14:19 VishVish 4,49210 gold badges45 silver badges76 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 11

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.

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.

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 remend jquery validation plugin for client side validation.

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

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.

本文标签: javascriptHow to validate Forms in PHPStack Overflow