i wanto implement jquery.alphanumeric in html pattern, so i use jquery plugin from here,
this is my code
<input type="text" char-allow="$#*" class="alpha">
<input type="text" char-allow="+-" class="numeric">
<input type="text" char-allow="&" class="alphanumeric">
<script>
function setAlphaNumeric(){
$(".numeric").each(function(){
var a = $(this).attr("char-allow"); $(this).numeric({allow:a});
});
$(".alpha").each(function(){
var a = $(this).attr("char-allow"); $(this).alpha({allow:a});
});
$(".alphanumeric").each(function(){
var a = $(this).attr("char-allow"); $(this).alphanumeric({allow:a});
});
}
$(function(){
setAlphaNumeric();
});
</script>
is there any shortest & best than that code?
FYI, with jQuery and a little simple use of RegEx, you really don't need a plugin for this. It's extremely simple.
$(document).on: Here, I'm simply making use of the DOM'sdocumentvariable to assign events via jQuerylivefeature. This is a handy way to ensure that even elements loaded after the DOM is finished loading (dynamic elements) will still adhere to the given event task..on('keydown', '.alpha': The first 2 params are our event and our selector, respectively. I chose thekeydownevent because we can control whether or not to even show a character by returning abooleanvalue. IFfalseis returned, then no character will be printed to the UI.var a = e.key;: jQuery Events is nice enough to translate to String what character is being asked for. Here, I simply assign that String to a variable for checking!if (a.length == 1): The reason for this is simple, if the String is longer than 1 or less than 1, then it's not a character. You can see this yourself by using something likeconsole.debug(a);in the Event method, and then watching your console as you type in the input. For example, a Backspace key will have a String of"Backspace".The RegEx's:
/[a-z]|\$|#|\*/i: alpha[a-z]: Looks for all lower case lettersathroughz|\$|#|\*: Includes your "allowed" characters, using|as an "OR" separator. A Backslash is needed for the$and*characters in order to escape them in the RegEx statements; otherwise they have an entirely different meaning of their own./i: This little suffix tells our RegEx to ignore the case of the characters. So our beginning statement no longer cares if it's Lower or Upper case./[0-9]|\+|-/: numeric[0-9]: Looks for each digit,0through9|\+|-: Much the same as the alphabet one, this is your extra "allowed" characters, separated by an|(OR) operator./[a-z]|[0-9]|&/i: alphanumeric[a-z]: Looks for all lower case lettersathroughz|[0-9]: Or looks for each digit,0through9|&: Again, checks for your allowed character./i: And again, ignore all case issues.Example 1:
Or with everything combined in one method, Example 2:
†Most Simplistic Version:
This first version is barely 20 lines of code, and could even be minified to be much smaller [QuickLink]. It's very simplistic in that you only need to assign 1 of 3 classes to any input,
[ 'alpha', 'numeric', 'alphanumeric' ]. If you want extra characters allowed, then simply add an HTML5 data attribute of "allow" and give it the value of the extra characters to allow.Like so:
<input class="alpha" data-allow="$#*" type="text" />‡Extended Version:
This version is a little more involved. Still pretty simplistic in that you need only assign classes and use the
data-allow="string"attribute to allow extras. However, the main difference is that this version has its ownObjectthat can betoggledon and off. It also has a feature in the very first line that allows you to set it to initially beonoroffbased on whether or not the first variable istrueorfalse. This version will also automatically clean the value of alpha|numeric inputs when toggled on.