復制代碼 代碼如下:
/檢查非法字符並檢驗字符長度
function checkSpeChar(obj, byteLength, title) {
var value = document.getElementById(obj).value;
value = value.replace(/(^\s*)/g, "").replace(/(\s*$)/g, "");
var ret = (/[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']/.test(value));
var arr = ["@", "#", "$", "%", "^", "&", "*", "<", ">", "/", "\\", "\'", "'", ".", ":", "?", "[", "]", ";", "-", "、", "(", ")", "_", "\"", "~", "insert", "delete", "update", "select"];
value = value.toLowerCase();
for (var i = 0; i < arr.length; i++) {
if (value.indexOf(arr[i].toLowerCase()) >= 0) {
ret = true;
}
}
if (ret == true) {
top.LSAlert("內容中含非法字符,請重新輸入!");
document.getElementById(obj).value = '';
return;
}
limitLength(value, byteLength, title, obj);
}
//驗證輸入字符長度
function limitLength(value, byteLength, title, attribute) {
value = value.replace(/\r/g, " ").replace(/\n/g, " "); //回車換行分別替換兩個空格
var newvalue = value.replace(/[^\x00-\xff]/g, "***");
var length = newvalue.length;
//當填寫的字節數小於設置的字節數
if (length * 1 <= byteLength * 1) {
return;
}
var limitDate = newvalue.substr(0, byteLength);
var count = 0;
var limitvalue = "";
for (var i = 0; i < limitDate.length; i++) {
var flat = limitDate.substr(i, 1);
if (flat == "*") {
count++;
}
}
var size = 0;
var istar = newvalue.substr(byteLength * 1 - 1, 1); //校驗點是否為“×”
//if 基點是×;
if (count % 3 == 0) {
size = count / 3 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
else if (count % 3 == 1) {
size = (count - 1) / 3 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
else if (count % 3 == 2) {
size = (count - 2) / 3 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
//top.LSAlert(title + "最大輸入" + byteLength + "個字節!");
document.getElementById(attribute).value = limitvalue;
return;
}