function allowCharacters(evt, strAllowedChars) {
// eg. allowCharacters('0-9|-|,|(|)') Will allow 0 to 9, -,()|
var charCode = (evt.which) ? evt.which : event.keyCode
var keycodeval = "";
var finalkeycode = "";
if (strAllowedChars != "" || strAllowedChars != null || strAllowedChars.indexOf("|") != -1) {
XstrAllowedChars = strAllowedChars.split("|");
for (strcount = 0; strcount <= XstrAllowedChars.length - 1 ; strcount++) {
if (XstrAllowedChars[strcount].search("-") != -1) {
substr = XstrAllowedChars[strcount].split("-");
if (substr[0] != substr[1]) {
if (substr[0].charCodeAt(0) <= substr[1].charCodeAt(0)) {
codeval = "(charCode >= " + substr[0].charCodeAt(0) + " && " + "charCode <= " + substr[1].charCodeAt(0) + ")";
keycodeval = keycodeval + "||" + codeval;
}
else {
codeval = "(charCode <=" + substr[0].charCodeAt(0) + " && " + "charCode >= " + substr[1].charCodeAt(0) + ")";
keycodeval = keycodeval + "||" + codeval;
}
}
else if (substr[0] == "" && substr[1] == "") {
keycodeval = keycodeval + "|| " + " charCode == 45 " + " || " + " charCode == 32";
}
}
else {
codeval = "(charCode == " + XstrAllowedChars[strcount].charCodeAt(0) + ")";
keycodeval = keycodeval + "||" + codeval;
}
}
}
newval = keycodeval.slice(2, keycodeval.length);
var retrunflag = false;
if (eval(newval)) {
//var charCode1 = (evt.which) ? evt.which : event.keyCode
if (event.keyCode != 60 && event.keyCode != 62 && event.keyCode != 94 && event.keyCode != 38) {
retrunflag = true; //event.keyCode = event.keyCode;
}
else
retrunflag = false; //event.keyCode = 0;
}
else {
retrunflag = false; //event.keyCode = 0 ;
}
//alert(retrunflag);
return retrunflag;}
OR
function funRestrictSpecialChar(Obj, evt) {
var inputVal = $("#" + Obj.id).val();
re = /[`[email protected]#$%^&*()_|+\-=?;:'"<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(inputVal);
if (isSplChar) {
var no_spl_char = inputVal.replace(/[`[email protected]#$%^&*()_|+\-=?;:'"<>\{\}\[\]\\\/]/gi, '');
$("#" + Obj.id).val(no_spl_char);
} }
這樣的變量是值在很多地方使用全局變量。使用該變量只有我必須檢查名字 – Bhargavi