How can i restrict '♥♣' like characters from saving into database. If these characters appear in name text field, a error message should be thrown. I am using ruby on rails.
Thanks, Anubhaw
How can i restrict '♥♣' like characters from saving into database. If these characters appear in name text field, a error message should be thrown. I am using ruby on rails.
Thanks, Anubhaw
See this for an example of allowing only a specific set of characters (whitelisting), 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;
}
}
};
From: prevent typing non ascii characters in a textbox
Alternately you can use regex to strip out non ascii characters.
see here: How to remove all non - ASCII characters from a string in Ruby
You want some javascript to tell users when they put such characters in the form. You need a validation routine in your RoR code to make sure that only acceptable characters are actually placed in the database.