This article mainly introduces JS filter special character input suitable for input, paste, drag and drop a variety of situations, the need for friends can refer to the following
Code as follows: function textvalidate (type) { var code; var character; var Lang = document.getElementById (' Lang ') .value; var err_msg = ""; if (lang!= "Eng") { err_msg = "folder name cannot contain one of the following characters: 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 = =" P Aste ") { code = window.clipboardData.getData (' Text '); } else if (type =" Drop ") { code = W Indow.event.dataTransfer.getData (' Text '); } Else { code = arguments.callee.caller.arguments[ 0].which; } var character = String.fromCharCode (code); var txt = new RegExp ("[*,&,,/,?, |,:,<,& gt;, "]"; if (type = = "Input") { if (txt.test (character)) { alert (err_msg); if (document.all) {&NB Sp Window.event.retUrnvalue = false; } Else { arguments.callee.caller.arguments[0].preventdefault (); } } } if (type = = "Paste" | | | type = = "Drop") { if (Txt.test (code)) { alert (err_msg); Window.eve Nt.returnvalue = false; } } }