Ok basically when I type , it won't allow it.
I want to add more like < > / \ etc how do I do it?
$("#in1").keypress(function (evt) {
if (String.fromCharCode(evt.which) == ",")
return false;
});
<input type="text" id="in1">
Can see the demo here. http://jsfiddle.net/QshDd/38/
If you have a list of disallowed characters, you can forbid them in this fashion:
$("#in1").keypress(function (evt) {
return [',', '<', '>', ].indexOf(String.fromCharCode(evt.which)) === -1;
});
its working , you Required to give more conditions:
$("#in1").keypress(function (evt) {
if (String.fromCharCode(evt.which) == ",")
return false;
if (String.fromCharCode(evt.which) == "<")
return false;
if (String.fromCharCode(evt.which) == ">")
return false;
if (String.fromCharCode(evt.which) == "\\")
return false;
});
Another Solution , either use regEx or use XMLParser or JSON parser methods.
if you want something like this
<input type="text">
===> input typetext
$("#in1").keypress(function (evt) {
if (isValid(String.fromCharCode(evt.which)))
return false;
});
function isValid(str){
return /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}
http://jsfiddle.net/QshDd/61/
Try this, if you need only alphabets
$("#in1").keypress(function (evt) {
var expr = /^([a-zA-Z\s]+)$/i;
if (!String.fromCharCode(evt.which).match(expr))
return false;
});
var chars = new Array("<", ">" ,"/" ,"\\");
$("#in1").keypress(function (evt) {
if(!(chars.indexOf(String.fromCharCode(evt.which)) > -1))
return false;
});