Filter Input--JS Summary
1. Filter the input two modes:
1.1 Prohibit or block input of non-numeric keys, prevent the default behavior of non-numeric keys
1.2 After the verification is canceled, you can first enter the illegal characters, then judge, cancel the text just entered
2. Achieve:
2.1 Method: Forbid ... Default behavior"Reference encapsulation: encapsulating the underlying code"
addevent (window, ' Load ', function () {var fm = document.getElementById (' MyForm '); var user = fm.elements[' user ']; var content = fm.elements[' content '; Addevent (content, ' keypress ', function (evt) {var e = evt | | window.event; var charcode = Getcharcode (evt); The regular expression to obtain whether the text is a digital//alert (String.fromCharCode (charcode)); if (!/\d/.test (String.fromCharCode (charcode)) && charcode > 8) {predef (evt); Shielding non-numeric keypad input}//ps:charcode > N, N is limited to releasing the cursor key, backspace and delete key, n too large, will appear through the net. }); Addevent (Areafield, ' cut ', function (evt) {predef (evt); Block cropping}); Addevent (Areafield, ' Copy ', function (evt) {predef (evt); Block replication}); Addevent (Areafield, ' paste ', function (evt) {predef (evt); Block paste}); Block Chinese input method, but chrome invalid Content.style.imeMode = ' disabled '; "Noteworthy": shielding Input Method User experience is not good, will let the user mistakenly think the computer is broken});
2.2 method: After verifying ... The text entered"Reference encapsulation: encapsulating the underlying code"
addevent (window, ' Load ', function () { var fm = document.getElementById (' MyForm '); var user = fm.elements[' user ']; var content = fm.elements[' content '; Addevent (content, ' keypress ', function (evt) { var e = evt | | window.event; var charcode = Getcharcode (evt); The regular expression to obtain whether the text is a digital //alert (String.fromCharCode (charcode)); if (!/\d/.test (String.fromCharCode (charcode)) && charcode > 8) { predef (evt); Block input of non-numeric keypad } //"note": CharCode > N, N is limited to release cursor key, backspace and delete key, n too large, will appear through the net. }); Addevent (content, ' KeyUp ', function (evt) { this.value = this.value.replace (/[^\d]/g, '); Replace non-numeric keys with empty });
In SQL database security, we are particularly familiar with anti-' SQL injection ', and ' SQL injection ' is mainly used in the English half-width of the single quotation mark "'", we can do the appropriate settings, through the regular expression to filter out.
Filter Input--JS Summary