方法一:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script language ="javascript"> function ValidateValue(textbox) { var IllegalString = "\`~@#;,.!#$%^&*()+{}|\\:\"<>?-=/,\'"; var textboxvalue = textbox.value; var index = textboxvalue.length - 1; var s = textbox.value.charAt(index); if(IllegalString.indexOf(s)>=0) { s = textboxvalue.substring(0,index); textbox.value = s; } } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Text1" runat="server" type="text" onkeyup = "ValidateValue(this)"/></div> </form> </body></html>
方法二:
function stripScript(str) {
var pattern = new RegExp("[%--`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——| {}【】‘;:”“'。,、?]") //格式 RegExp("[在中间定义特殊过滤字符]")
var s = str.value;
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs+s.substr(i, 1).replace(pattern, '');
}
str.value = rs;
}