确保表单中的文本输入是 unicode,否则会引发错误
我在这里读过其他几个类似的问题,但没有找到我要找的东西。我想知道无论如何,确保表单内的文本输入是 unicode 的最简单方法是什么。我正在使用 django 和大量前端 javascript,这在我看来是做到这一点的最佳方法。我可以自己做这件事,但恐怕我计划做的方式不是最好的方式。
I have read a couple other similar questions on here about this but didnt find what I was looking for. I am wondering what is the simplest way to ensure that no matter what, the text input inside of a form is unicode. I am using django and alot of front-end javascript which seems to me the best way to do this. I could do this myself but I am afraid that the way I plan on doing it is not the best way possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Web 浏览器表单字段的内容采用本机 Unicode 格式;没有任何东西可以以非 Unicode 的形式放入。
您可能需要执行一些检查,以确保没有控制字符、显式非字符、由 Unicode/W3 表示为“不适合在标记中使用”的字符或无效使用代理项,但这些是您需要进行的检查必须在服务器端完成。无论如何,你都必须在服务器端进行有效性检查;在客户端检查这些问题没有任何好处,而且这些问题通常不是普通用户能够意外键入的内容。
至于服务器端检查提交给表单的字节流是否以正确的方式转换为 Unicode 字符串,这取决于您的框架。例如,Django 使用 DEFAULT_CHARSET(通常是 UTF-8)来实现。
The content of web browser form fields in natively Unicode; there is nothing you could put in a form that would not be Unicode.
There are some checks you might want to do to ensure that you don't have control characters, explicit non-characters, characters denoted by Unicode/W3 as “unsuitable for use in markup” or invalid use of surrogates, but those are checks you'd have to do on the server side. You have to do validity-checking on the server side anyway; there is no benefit to checking for these problems on the client side as well as these are not generally things the average user would be able to type by accident.
As for server-side checking that the stream of bytes submitted for the form is converted into a Unicode string in the proper way, that would be up to your framework. eg Django does it with the DEFAULT_CHARSET (usually UTF-8).