input prohibited to enter special characters

One way: After the value of the processing to get the value before you pass

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;
}

This function can be called directly.

Second way: the most fundamental level is not prompt the user to enter special characters

function showKeyPress(evt) {
evt = (evt) ? evt : window.event
return checkSpecificKey(evt.keyCode);
}

function checkSpecificKey(keyCode) {
var specialKey = "[`~!#$^&*()=|{}':;',\\[\\].<>/?~!#¥……&*()——|{}【】‘;:”“'。,、?]‘’";//Specific Key list
var realkey = String.fromCharCode(keyCode);
var flg = false;
flg = (specialKey.indexOf(realkey) >= 0);
if (flg) {
// alert('请勿输入特殊字符: ' + realkey);
return false;
}
return true;
}
document.onkeypress = showKeyPress;

Use: Add an event οnkeypress on input controls = "showKeyPress ()", when he will enter the reaction did not

 

This seems to happen a little problem at Chinese state so they do not know how to find a

Three ways: onkeyup after the event to get a similar manner to match with

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 = after "ValidateValue (this)" He will enter will immediately disappear, the user can see after I entered just said no input, the drawback is the continuous input it would not have gone (hold it there)

 

The next most Niubi more than one way in and

Four ways: directly on the control being

// <input οnkeyup = "value = value.replace (/ [\ W] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text'). Replace (/ [^ \ d] / G, '')) ">
// can only control the input box to enter text or numbers, can also be allowed to enter special characters are not allowed here following characters: (like @ # $% ^ & * etc.)! <br>

In this way the user can see the input will immediately disappear third approach is similar but with continuous input valid

Guess you like

Origin www.cnblogs.com/JurasVon/p/11684087.html