Filter Input--JS Summary

Source: Internet
Author: User
Tags delete key

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.