简体   繁体   English

验证字符是否可以在 Javascript 中使用 windows-1250 编码

[英]Verify if character may be encoded with windows-1250 in Javascript

How to verify if given character might be encoded (exists) in windows-1250 in Javascript如何验证给定字符是否可能在 Javascript 的 windows-1250 中编码(存在)

I need to introduce allowed characters validation in html inputs我需要在 html 输入中引入允许的字符验证

I solved this problem by creating an array of unicode character codes which correspond to windows-1250 characters.我通过创建对应于 windows-1250 字符的 unicode 字符代码数组解决了这个问题。 Here is my implementation:这是我的实现:

import * as _ from 'lodash';

const WINDOWS_1250_CHAR_CODES = [
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
  47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
  77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
  106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
  119, 120, 121, 122, 123, 124, 125, 126, 127, 160, 164, 166, 167,
  168, 169, 171, 172, 173, 174, 176, 177, 180, 181, 182, 183, 184,
  187, 193, 194, 196, 199, 201, 203, 205, 206, 211, 212, 214, 215,
  218, 220, 221, 223, 225, 226, 228, 231, 233, 235, 237, 238, 243,
  244, 246, 247, 250, 252, 253, 258, 259, 260, 261, 262, 263, 268,
  269, 270, 271, 272, 273, 280, 281, 282, 283, 313, 314, 317, 318,
  321, 322, 323, 324, 327, 328, 336, 337, 340, 341, 344, 345, 346,
  347, 350, 351, 352, 353, 354, 355, 356, 357, 366, 367, 368, 369,
  377, 378, 379, 380, 381, 382, 711, 728, 729, 731, 733, 8211, 8212,
  8216, 8217, 8218, 8220, 8221, 8222, 8224, 8225, 8226, 8230, 8240, 8249, 8250, 8364, 8482
];

export function windows1250encodingValidator(value: string): boolean {
  if (_.isEmpty(value)) {
    return true;
  }

  for (let i = 0; i < value.length; i++) {
    const charCode = value.charCodeAt(i);
    if (!WINDOWS_1250_CHAR_CODES.includes(charCode)) {
      return false;
    }
  }

  return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM