TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Avoid Special Characters on Key press using JavaScript
Surya Prakash
Aug 19
2015
Code
6.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
HTML
<
input
type
=
"text"
onkeypress
=
"avoidSplChars(event)"
>
JavaScript function
function avoidSplChars(e) {
e = e || window.
event
;
var bad = /[^\sa-z\d]/i,
key = String.fromCharCode(e.keyCode || e.which);
if
(e.which !== 0 && e.charCode !== 0 && bad.test(key)) {
e.returnValue =
false
;
if
(e.preventDefault) {
e.preventDefault();
}
}
}
Hope this helps!!
Avoid Special Characters
JavaScript