首頁 >web前端 >js教程 >js過濾特殊字元輸入適合輸入、貼上、拖曳多種情況_javascript技巧

js過濾特殊字元輸入適合輸入、貼上、拖曳多種情況_javascript技巧

WBOY
WBOY原創
2016-05-16 16:54:421198瀏覽
複製程式碼 程式碼如下:

function TextValidate(type) {
var. var character;
var Lang = document.getElementById('Lang').value;
var err_msg = "";
if (Lang != "Eng") {
err_msg = "資料夾名稱不能包含下列字元之一:n \ / : * ? " | & , ";
}
else {
err_msg = "A Folder Name cannot contain any of the following characters: n \ / : * ? " | & ,";
}

if (type == "input") {
code = window.event.keyCode;
}
else if (type == "paste") {
code = window.clipboardData.getData('Text');

}
else if (type == "Drop" ) {
code = window.event.dataTransfer.getData('Text');

}
else {
code = arguments.callee.caller.arguments[0].which;
}
var character = String.fromCharCode(code);
var txt = new RegExp("[\*,\&,\\,\/,\?,\|,\:,\ ,"]");
if (type == "input") {
if (txt.test(character)) {
alert(err_msg);
if (document.all) {
window.event.returnValue = false;
}
else {
arguments.callee.caller.arguments[0].preventDefault();
}
}
}
}
if (type == "paste" || type == "Drop") {
if (txt.test(code)) {
alert(err_msg);
window.event.returnValue = false;
}
}
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn