我們如何使用java腳本來限制在特定文本字段中使用非ASCII字符..?在此先感謝...用於檢測非ASCII字符的Java腳本正則表達式
5
A
回答
16
ASCII是指在000-177(八進制)範圍內的字符,因此
function containsAllAscii(str) {
return /^[\000-\177]*$/.test(str) ;
}
你可能不想接受非打印字符\000-\037
,也許你的正則表達式應該是/\040-\0176/
+2
如果所有你想要的是一個布爾值,你應該使用'.test()'而不是'.exec()' - 它直接產生一個布爾值,而不是構建一個匹配對象,然後必須被轉換爲布爾值。 – 2011-03-03 20:02:59
+0
感謝Ben,我懶得找到正確的方法。根據你的建議修正 – 2011-03-03 20:05:12
1
我來到這個頁面試圖尋找一個函數來淨化一個字符串在CMS系統中用作友好的URL。 CMS是多語言的,但我想阻止非ASCII字符出現在URL中。因此,我不是使用範圍,而是簡單地使用(基於上述解決方案):
function verify_url(txt){
var str=txt.replace(/^\s*|\s*$/g,""); // remove spaces
if (str == '') {
alert("Please enter a URL for this page.");
document.Form1.url.focus();
return false;
}
found=/^[a-zA-Z0-9._\-]*$/.test(str); // we check for specific characters. If any character does not match these allowed characters, the expression evaluates to false
if(!found) {
alert("The can only contain letters a thru z, A thru Z, 0 to 9, the dot, the dash and the underscore. No spaces, German specific characters or Chinese characters are allowed. Please remove all punctuation (except for the dot, if you use it), and convert all non complying characters. In German, you may convert umlaut 'o' to 'oe', or in Chinese, you may use the 'pinyin' version of the Chinese characters.");
document.Form1.url.focus();
}
return found;
}
相關問題
- 1. Swift:檢測非字母數字字符的正則表達式
- 2. 用於檢查非轉義字符的正則表達式
- 3. 正則表達式找出python中非ascii字符的字段
- 4. 如何在非ASCII字符串中使用正則表達式?
- 5. 正則表達式任意ASCII字符
- 6. 正則表達式查找ASCII字符
- 7. [Java腳本]正則表達式表達
- 8. 用於檢測查詢字符串的正則表達式
- 9. 用於檢測字符串內重複的正則表達式
- 10. 的Javascript正則表達式來拒絕非ASCII美國字符
- 11. 非標準ASCII字符的正則表達式
- 12. 正則表達式來檢測幾個非字符
- 13. (grep)正則表達式匹配非ASCII字符?從WINDOWS
- 14. (grep)正則表達式匹配非ASCII字符?
- 15. 正則表達式的正則表達式的Java字符串
- 16. 正則表達式來檢測由非字母字符分隔的字符串
- 17. TCL正則表達式,用於檢測非空白
- 18. 非字符串的正則表達式
- 19. Java腳本數字格式不正常的正則表達式
- 20. 正則表達式的Java非字母數字字符匹配
- 21. .Net正則表達式匹配任何非ASCII字符的字符串
- 22. Java的正則表達式:檢查是否字有非字母數字字符
- 23. Java正則表達式用連字符
- 24. 正則表達式不檢測符號
- 25. 用於非字母和非數字的正則表達式
- 26. Java的正則表達式字符串
- 27. java正則表達式支持非ascii值?
- 28. 非ASCII的情況下正則表達式字邊界
- 29. 用於檢查子字符串的正則表達式是否具有所有數字(在Java腳本中)
- 30. Java中的正則表達式在Shell腳本中的正則表達式
是否要將它們移除或替換? – drudge 2011-03-03 19:14:58
Dup:http://stackoverflow.com/questions/3465874/javascript-regex-to-reject-non-ascii-us-characters(我不參與投票) – 2011-03-03 19:15:07
@jnpcl只需提醒用戶即可。 .....刪除它們也是一個不錯的選擇 – sasidhar 2011-03-03 19:15:55