[英]How can i validate Greek characters in javascript?
我正在使用這段 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;
}
但它沒有結果。
你的方法沒問題。 您的代碼中的其他地方一定有問題。
也許只有在沒有希臘字符時才滿足case "alpha_s"
。 或者您的TestInputType()
函數可能被匹配字符串參數中的太多反斜杠弄糊塗了。 或者您的文檔可能沒有使用 Unicode。
以下代碼段似乎有效:
◽️更新(2021 年 4 月 28 日):我擴展了匹配字符類以包含 Unicode 塊
\Ͱ
到\Ͽ
(希臘語和科普特語)和\ἀ to \
(希臘語擴展)中的任何內容。
function testChars() { var str = document.getElementById("txt").value; var r = document.getElementById("result"); // Allow Roman alphabet characters (AZ, az), // plus anything in the Unicode blocks \Ͱ to // \Ͽ (Greek and Coptic) and \ἀ to \ // (Greek Extended): if (/^[A-Za-z\Ͱ-\Ͽ\ἀ-\]*$/.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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.