mysql - 如何在文本框中限制 '♥♣'个字符

标签 mysql html ruby-on-rails

我如何限制'♥♣'之类的字符保存到数据库中。如果这些字符出现在名称文本字段中,则应抛出一条错误消息。 我在 Rails 上使用 ruby​​。

谢谢, 阿努伯

最佳答案

有关仅允许一组特定字符(白名单)的示例,请参阅此示例,IMO 更好更安全:

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

来自: prevent typing non ascii characters in a textbox

或者,您可以使用正则表达式去除非 ascii 字符。

请看这里:How to remove all non - ASCII characters from a string in Ruby

关于mysql - 如何在文本框中限制 '♥♣'个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666576/

相关文章:

php - WordPress 在数据库中查找和替换

mysql - 使用SQL计算时间序列中的时间间隔

mysql - Mysql Insert语句的基本查询

css - 您将如何设置此 DIV 布局(左侧右对齐 div,右侧左对齐 Div)?

ruby-on-rails - 如何使用 rspec 从 gem stub 模块方法

c# - 这两个 C# 方法有什么区别

JavaScript 重定向到错误页面

javascript - jQuery 动画滞后

ruby-on-rails - Rufus-时区调度程序变量 - ruby​​ on Rails

ruby-on-rails - 如何让 postgres 在 big sur 上启动?