如何在javascript中验证希腊字符?

时间:2018-05-13 19:51:03

标签: javascript validation

我正在使用这段javascript代码来验证字母字符

   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;
}

但它没有结果。

1 个答案:

答案 0 :(得分:0)

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

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

以下代码段似乎有效:

function testChars() {
  var str = document.getElementById("txt").value;
  var r = document.getElementById("result");
  if (/^[A-Za-z\u0391-\u03C9]*$/.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>