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);
}
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;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.