HTML5 验证与 PHP 验证

发布于 2024-12-19 23:26:23 字数 102 浏览 0 评论 0原文

当涉及到表单验证时,最好的方法是什么?使用 HTML5,您可以验证电子邮件地址和电话号码,但通常我会使用 PHP 来验证,那么哪种方法更好?或者我应该验证 HTML5 和 PHP 中的输入?

When is comes to form validation, which is the best method to proceed with? With HTML5, you can validate the e-mail address and telephone number but usually I would do this with PHP, so which method is better? Or should I validate the input in HTML5 and PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

溇涏 2024-12-26 23:26:23

两个都。

经验法则:
验证客户端,为用户提供更好的体验。
验证服务器端以确保您收到的数据 100% 符合您的预期。

Both.

Rule of thumb:
Validate client-side to provide users with a better experience.
Validate server-side to ensure 100% the data you receive is what you expect.

趴在窗边数星星i 2024-12-26 23:26:23

始终使用服务器端编程语言进行验证,此外您还可以使用客户端验证。原因是所有内容都可以在客户端进行修改,从而绕过任何客户端验证。这并不难,而且有很多工具可供人们修改 html 和/或 Javascript 以提交他们想要的任何内容。

黄金法则是,永远不要相信用户。做好最坏的打算并做好准备。您可以并且应该使用firebug或其他Web开发人员工具等工具来尝试。

Always validate with a server side programming language, additionally you could use a client side validation. The reason for this is that everything can be modified client side and thus bypass any client side validation. It's not even hard and there are a lot of tools available for people to modify the html and/or Javascript to submit whatever they want.

The golden rule is, never trust the user. Expect the worst and prepare for that. You can and should use tools such as firebug or other web developer tools to try things out.

弄潮 2024-12-26 23:26:23

你应该两者都做。客户端的 HTML5 和/或 JavaScript 验证可以节省用户的页面加载时间,而 PHP 验证则因为您永远无法完全信任客户端。

You should do both. HTML5 and/or JavaScript validation on the client-side to save the user a page load, and PHP validation because you can never, ever fully trust the client-side.

好听的两个字的网名 2024-12-26 23:26:23

我更喜欢两者。填写表格时如果有不匹配的地方就会收到通知,这很好。但是,您不能依赖 JS 验证,因为它很容易被绕过,因此需要服务器端。

I prefer both. It is nice when filling out a form to be notified if something doesn't match. However you can't rely on JS validation as it is easily bypassed so server side is required.

若能看破又如何 2024-12-26 23:26:23

答案都是正确的。不是一个偏好(正如一些人所说)而是一个要求。所有 HTML(包括 HTML5)都可以通过浏览器直接在您的页面上进行编辑。就像使用 Firebug 编辑此页面的标题一样,您可以编辑 HTML 输入验证器,如果它们没有服务器端验证,它将接受您输入的任何内容。仅使用 HTML5 处理验证就像邀请黑客破坏您的所有数据库表一样。你想要那个吗?

我在这个愚蠢的家伙的网站上发现了最后一条评论作为一个明显的例子: http://demosthenes.info/blog/462/Goodbye-JQuery-Validation-HTML5-Form-Errors-With-CSS3

The answer both is correct. Not a preference (as some have stated) but a requirement. All HTML (including HTML5) can be edited directly on your page via the browser. Just like using Firebug to edit this page's title, you can edit the HTML input validators and if they don't have server-side validation, it will accept anything you put in there. Handling validation with HTML5 only is like inviting hackers to corrupt all of your db tables. Do you want that?

I found this last comment on this stupid guy's site as a clear example: http://demosthenes.info/blog/462/Goodbye-JQuery-Validation-HTML5-Form-Errors-With-CSS3

圈圈圆圆圈圈 2024-12-26 23:26:23

大多数专业程序员都采用服务器端脚本语言验证输入字段的方法。使用 PHP 的服务器端表单验证在很多方面都优于 HTML5 和客户端表单验证:

  1. HTML5 包含一些自动验证输入字段的属性,例如:电子邮件、电话等。这些属性是有限的。而在现实世界中,有许多表单的某些字段无法通过 HTML5 直接验证。

  2. 如果您正在考虑使用 javscript 作为第二个选项进行客户端验证,请记住,可以在浏览器中关闭 javascript。因此它最终会影响用户体验。因此最好通过服务器端脚本语言(例如 PHP)验证您的表单。

  3. 对于包含敏感数据的网站,您应该采用上述方法。但

Most of the professional programmers have the approach of validating input fields with server side scripting language. Server side form validation using PHP is better than HTML5 and client side form validations in a number of ways:

  1. HTML5 contains few attributes which automatically validates input fields for example:email,telephone,etc. These attribtues are limited. whereas in real world, there are many forms which have some fields which cant be directly validated through HTML5.

  2. If you are thinking about clients side valdiation using javscript as the second option then keep in mind that javascript can be turned off in browser. so it will ultimately affect the user experience.so its better to valdiate your forms through server side scripting lanugae like PHP.

  3. You should adopt the above approach for websites containing sensitive data. but

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文