jQuery AlphaNumeric
HTML
<input type="text" class="sample1 tb" size="10"/>
JavaScript
(function ($) {
$.fn.alphanumeric = function (p) {
var input = $(this),
az = "abcdefghijklmnopqrstuvwxyz",
options = $.extend({
ichars: '!@#$%^&*()+=[]\\\';,/{}|":<>?~`.- _',
nchars: '',
allow: ''
}, p),
s = options.allow.split(''),
i = 0,
ch,
regex;
regex = new RegExp(options.allow, 'gi');
ch = (options.ichars + options.nchars).replace(regex, '');
input.keypress(function (e) {
var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (ch.indexOf(key) != -1 && !e.ctrlKey) {
e.preventDefault();
}
});
input.blur(function () {
var value = input.val(),
j = 0;
for (j; j < value.length; j++) {
if (ch.indexOf(value[j]) != -1) {
input.val('');
return false;
}
}
return false;
});
return input;
};
$.fn.numeric = function (p) {
var az = 'abcdefghijklmnopqrstuvwxyz',
aZ = az.toUpperCase();
return this.each(function () {
$(this).alphanumeric($.extend({
nchars: az + aZ
}, p));
});
};
$.fn.alpha = function (p) {
var nm = '1234567890';
return this.each(function () {
$(this).alphanumeric($.extend({
nchars: nm
}, p));
});
};
})(jQuery);
$('.sample1').alphanumeric();