Below is my javascript code which allows only alphabets while entering if we enter numbers and some special characters it does not accept them while entering only this works fin
You can use regex to test your input , with something like this :
var regexp = new RegExp(/^[a-zA-Z0-9 ]+$/);
var str="sqdqsd qsd125";
var str2="sqdqsdqsd12;5";
console.log(regexp.test(str));
console.log(regexp.test(str2));
https://jsfiddle.net/Tintin37/7Lj7r9zp/1/
EDIT
I added a little snippet ;)
function onlyAlphabets(e, t) {
var regexp = new RegExp(/^[a-zA-Z0-9 ]*$/);
console.log(regexp.test(t.value));
return regexp.test(t.value);
}
EDIT2
With the desired behavior ;)
function onlyAlphabets(e, t) {
var regexp = new RegExp(/^[a-zA-Z0-9 ]*$/);
if (window.event) {
keynum = e.keyCode;
} else if (e.which) {
keynum = e.which;
}
var test = regexp.test(String.fromCharCode(keynum));
return test;
}