How do I block special characters from being typed into an input field with jquery?
views:
2576answers:
3
+1
A:
Use a jQuery Validation plugin, and allow only alphanumerical characters.
Tomas Lycken
2009-05-21 22:59:23
That means the form won't send. It won't restrict people from entering certain characters
nickf
2009-05-21 23:13:05
It could easily be used to validate each change, simply by hooking the .validate() method to the .keyup() event (or similar).
Tomas Lycken
2009-05-21 23:22:07
A:
this is an example that prevent the user from typing the character "a"
$(function() {
$('input:text').keydown(function(e) {
if(e.keyCode==65)
return false;
});
});
key codes refrence here:
http://www.expandinghead.net/keycode.html
Marwan Aouida
2009-05-21 23:02:35
+3
A:
Take a look at the jQuery alphanumeric plugin. http://itgroup.com.ph/alphanumeric/
//All of these are from their demo page
//only numbers and alpha characters
$('.sample1').alphanumeric();
//only numeric
$('.sample4').numeric();
//only numeric and the .
$('.sample5').numeric({allow:"."});
//all alphanumeric except the . 1 and a
$('.sample6').alphanumeric({ichars:'.1a'});
You can take this a step further to by adding masked inputs...
RSolberg
2009-05-21 23:07:37