我如何限制'♥♣'之类的字符保存到数据库中。如果这些字符出现在名称文本字段中,则应抛出一条错误消息。 我在 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/