<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> Yesterday, the project search reported an error. The pain was from the production library. I looked at the log because the user entered special characters when searching. There is no way to get the fastest way to directly filter the data entered by the user and remove special characters</span>
Some special characters are passed into the background and will produce errors. It may be injected into SQL, so it is fundamentally intercepted.
Let’s discuss the way in which input prohibits input from entering special characters:
Method 1: After getting the value value, process it before you pass it
function stripscript(value) {var pattern = new RegExp("[`~!@#$^&*()=|{}':;',//[//].<>/?~!@#¥…&*()―|{}【】';: "'., , ?]") var rs = ""; for (var i = 0; i < value.length; i++) {rs = rs+s.substr(i, 1).replace(pattern, ''); } return rs;}Just call this function directly.
Method 2: The most fundamental way is to prompt the user to not be able to enter special characters
function showKeyPress(evt) {evt = (evt) ? evt : window.eventreturn checkSpecificKey(evt.keyCode);}function checkSpecificKey(keyCode) {var specialKey = "[`~!#$^&*()=|{}':;',//[//].<>/?~!#¥…&*()―|{}【】';: "'., , ?]''";//Specific Key listvar realkey = String.fromCharCode(keyCode);var flg = false;flg = (specialKey.indexOf(realkey) >= 0);if (flg) {// alert('Please do not enter special characters: ' + realkey);return false;}return true;}document.onkeypress = showKeyPress;Use: Adding events on the input control does not respond at all
It seems to be a little problem. There is no response in Chinese state. I don’t know what’s going on, so I found another one.
Method 3: After getting the onkeyup event, it matches it similar to the method.
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;}}Use: onkeyup = "ValidateValue(this)" It will disappear immediately after entering. The user can see that what I entered is just saying that it will disappear after entering. The disadvantage is that continuous input will not disappear (hold it down)
The next most awesome way to neutralize the above
Method 4: Use it directly on the control
// <input /[/W]/g,'') "/[^/d]/g,''))">// The control input box can only enter text or numbers, and special characters can be not allowed to enter the following characters here: (such as!@#$%^&*, etc.)<br>
This method users can see that the input will disappear immediately. The same is true for the method three, but the continuous input is valid.
Let’s take a look at the following js to determine that the characters entered cannot be special characters:
1. The document.onkeypress event is a verification when entering a character. The correspondence showKeyPress is executed during verification. When returning true, it can be entered, and false cannot be entered.
2. evt = (evt) ? evt : window.event, pressing the keyboard will generate this event to get the value you typed (it is the keyboard's keyCode).
var realkey = String.fromCharCode(keyCode);
The String.fromCharCode() method is to convert the keyboard value (keyCode) into the value you actually input.
3. Method specialKey.indexOf(realkey):
.indexOf() is a String method. The IndexOf() method of the string searches whether a string passed as a parameter appears on the string. If a string is found, it returns the starting position of the character (0 means the first character, 1 means the second character and so on). If it is not found, it returns -1
4. var specialKey = "#$%/^*/'/"/+";
Here are strings with escape characters, #, $, %, ^, *, ', ", + are special symbols, and the ones with / before need to be escaped.
If you want to add restricted input special symbols in the text box, use <input type="text" id="name" onkeypress="showKeyPress()">
The above are the four ways to implement input that prohibits the input from entering special characters. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!