Javascript根据所需的字符集验证用户输入(编码) [英] Javascript validate user input against desired character set (encoding)

查看:148
本文介绍了Javascript根据所需的字符集验证用户输入(编码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案如下:

用户使用Win-1252编码从网站复制其字符集的文本。然后将该文本发送到我使用ISO-8859-1字符集(这是Win-1252的子集)控制的数据库。 Javascript中是否有一个机制来通知用户他们正在尝试在系统中插入无效字符?首选项,如果它可以突出显示所述字符。

User copies text from web site using Win-1252 encoding for its character set. This text is then sent to a database that I control with a character set of ISO-8859-1(this is a subset of Win-1252). Is there a mechanism within Javascript to inform the user that they are trying to insert "invalid" characters into the system? Preference if it can highlight said characters.

这个问题的一般形式是系统A(发送系统)有一组编码定义为 AsubE 并且不同的系统B(接受系统)具有定义为 BsubE 的一组编码。当 BsubE AsubE 的范围内时,这不是问题。问题是当 BsubE 不是 AsubE 的子集时,我们如何验证用户的输入。 p>

The general form of this problem is that a system A(sending system) has a Set of encodings defined as AsubE and a different system B(the accepting system) has a set of encodings defined as BsubE. When BsubE is inside the universe of AsubE it is not a problem. The question is about when BsubE is not a subset of AsubE how can we validate the input from the user.

推荐答案

由于某些字符未在子集中定义,因此您可以使用正则表达式定义这些间隔:

Since some characters are not defined in the subset, you could use a regular expression to define those intervals:

function isNotAllowed(char) {
    return /\x00-\x1f|\x7f-\x9f/.test(char); // 00 to 1f, or 7f to 9f
}

变得更复杂,但这个功能可以是它的核心。

To also highlight characters it will become more complicated but this function could be the core of it.

这篇关于Javascript根据所需的字符集验证用户输入(编码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆