How to limit the characters "♥ ♣" in the text box

How can I limit "♥ ♣" as characters to save in the database. If these characters appear in the name text box, an error message must be raised. I use ruby ​​on rails.

Thanks Anubhaw

+3
source share
2 answers

See this example of using only a specific character set (whitelist), which IMO is better and safer:

var allowed = /[a-ZA-Z0-9]/; // etc.

window.onload = function () {
    var input = document.getElementById("test");

    input.onkeypress = function () {
        // Cross-browser
        var evt = arguments[0] || event;
        var char = String.fromCharCode(evt.which || evt.keyCode);

        // Is the key allowed?
        if (!allowed.test(char)) {
            // Cancel the original event
            evt.cancelBubble = true;
            return false;
        }
    }
};

C: prevent input of characters without ascii in the text field

Alternatively, you can use regex to highlight characters without ascii.

see here: How to remove all non-ASCII characters from a string in Ruby

+6

, - javascript , . RoR , , .

+1

Source: https://habr.com/ru/post/1763762/


All Articles