Limit certain characters in a text box?

I have a text box on a web page, and I would like to make it impossible for people to put certain characters in it, such as &, *, <, or>. Is there a way to do this with html? If not, I can also use PHP.

+4
source share
2 answers

If you used input, then there are several options available to you with the attribute type, for example color, date, email, number, tel. See https://developer.mozilla.org/en/docs/Web/HTML/Element/Input .

Since textareayou do not have these options. However, you can use JavaScript to listen for key events and to ignore some keystrokes. See http://jsfiddle.net/tg300eef/ .

var ta = document.getElementById("ta");
ta.addEventListener(
    'keypress',
    function (e) {
        // Test for the key codes you want to filter out.
        if (e.keyCode == 60) {
            alert('No "<"!');
            // Prevent the default event action (adding the
            // character to the textarea).
            e.preventDefault();
        }
    }
);

This, however, does not protect you from incorrect or malicious entries. That is, even if you use JavaScript to filter characters from a text field, it is still trivial for the user to circumvent this restriction (for example, disable JavaScript).

, , SQL- HTTP-, .. - . mysqli_real_escape_string MySQL, htmlspecialchars HTML, urlencode HTTP- .. , ( , ), .

, . , , . HTML, , <script> <iframe>, , <strong> <h1>. SQL .

, , , ( , ) , , , . , , Markdown, HTML .

, , . , 0 < x > 10, HTML 0 10 .

+4

Javascript, , , textarea. , , , :

<textarea rows="4" cols="50" onkeyup="this.value = this.value.replace(/[&*<>]/g, '')"></textarea>

JSFiddle: http://jsfiddle.net/81ox0vbr/6/

+4

All Articles