【问题标题】:How can i validate Greek characters in javascript?如何在 javascript 中验证希腊字符?
【发布时间】:2018-05-13 19:51:03
【问题描述】:

我正在使用这个 javascript 代码的 sn-p 来验证字母字符

   case "alpha_s":
            {
                ret = TestInputType(objValue, "([^A-Za-z\\s])", 
                strError, objValue.name + ": Only alphabetic characters and space 
                 allowed ");
                break;
            }

但我也需要能够验证希腊字符的代码。我试过这样做:

   case "alpha_s":
            {
                ret = TestInputType(objValue, "([^A-Za-z\\u0391-\\u03C9\\s])", strError, objValue.name + ": Only alphabetic characters and space allowed ");
                break;
}

但它没有结果。

【问题讨论】:

标签: javascript validation


【解决方案1】:

你的方法很好。您的代码中的其他地方一定有问题。

也许case "alpha_s" 只有在没有希腊字符时才会满足。或者您的 TestInputType() 函数可能被匹配字符串参数中的太多反斜杠弄糊涂了。或者您的文档可能没有使用 Unicode。

以下 sn-p 似乎有效:

◽️ 更新 (2021-04-28): 我已扩展匹配字符类以包含 Unicode 块 \u0370\u03FF(希腊语和科普特语)和 @987654325 中的任何内容@(希腊语扩展)。

function testChars() {
  var str = document.getElementById("txt").value;
  var r = document.getElementById("result");
  // Allow Roman alphabet characters (A-Z, a-z),
  // plus anything in the Unicode blocks \u0370 to
  // \u03FF (Greek and Coptic) and \u1F00 to \u1FFF
  // (Greek Extended):
  if (/^[A-Za-z\u0370-\u03FF\u1F00-\u1FFF]*$/.test(str)) {
    r.style.backgroundColor="green";
    r.style.color="white";
    r.innerHTML = "Looks OK";
  }
  else {
    r.style.backgroundColor="red";
    r.style.color="white";
    r.innerHTML = "Not OK!";
  }
}
<input type="text" id="txt" onkeyup="testChars()"
  placeholder="Type something" size="40">
<span id="result" style="padding:.1em .5em 0; border-radius:.5em"></span>

【讨论】:

  • @Naren 我已经更新了代码。立即尝试。
猜你喜欢
  • 2016-04-02
  • 2013-09-30
  • 2014-06-14
  • 2015-07-30
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多