Permalink
Browse files

added restrictInput option

  • Loading branch information...
1 parent 5ce21ba commit e0158855d5108f374268784cec9e17a138cbfd13 @Mottie committed Jan 29, 2011
Showing with 35 additions and 7 deletions.
  1. +4 −0 README.markdown
  2. +6 −1 index.html
  3. +5 −1 js/demo.js
  4. +18 −2 js/jquery.keyboard.js
  5. +2 −3 js/jquery.keyboard.min.js
View
@@ -45,6 +45,10 @@ Moved to the Wiki Pages: [Home][5] | [Setup][6] | [Options][7] ( [Layout][8], [L
Only the latest changes will be shown below, see the log to view older versions.
+Version 1.5.6
+
+* Added <code>restrictInput</code> option which when true will only allow keys that exist in the virtual keyboard to be typed/pasted into the window.
+
Version 1.5.5
* Removed jquery-ui class "ui-helper-hidden-accessible" as the recent change now prevents the popup from showing.
View
@@ -71,14 +71,19 @@
<div class="block">
<h2>Num Pad</h2>
<input id="num" class="alignRight" type="text" />
+ <br>
+ <small>
+ * input restricted.
+ </small>
</div>
<div class="block">
<h2>Custom: Hex</h2>
<input id="custom1" type="text" />
<br>
<small>
- * maxLength = 6.
+ * maxLength = 6.<br>
+ * input restricted.
</small>
</div>
View
@@ -12,7 +12,10 @@ $(document).ready(function(){
$('#alpha').keyboard({ layout: 'alpha' });
- $('#num').keyboard({ layout: 'num' });
+ $('#num').keyboard({
+ layout: 'num',
+ restrictInput : true
+ });
// Hex
$('#custom1').keyboard({
@@ -27,6 +30,7 @@ $(document).ready(function(){
]
},
maxLength : 6,
+ restrictInput : true,
useCombos : false // don't want A+E to become a ligature
});
View
@@ -1,6 +1,6 @@
/*
jQuery UI Virtual Keyboard Widget
-Version 1.5.5
+Version 1.5.6
Author: Jeremy Satterfield
Modified: Rob G (Mottie on github)
@@ -214,6 +214,9 @@ $.widget('ui.keyboard', {
// Prevents direct input in the preview window when true
lockInput : false,
+ // Prevent keys not in the displayed keyboard from being typed in
+ restrictInput: false,
+
// Set the max number of characters allowed in the input, setting it to false disables this option
maxLength : false,
@@ -461,13 +464,20 @@ $.widget('ui.keyboard', {
// check for key combos (dead keys)
_checkCombos : function(txt){
- var s = txt.length, o = this.options;
+ var i, s = txt.length, t, o = this.options;
if (o.useCombos) {
// keep 'a' and 'o' in the regex for ae and oe ligature (æ,œ)
txt = txt.replace(/([`\'~\^\"ao])([a-z])/ig, function(s, accent, letter){
return (accent in o.combos) ? o.combos[accent][letter] || s : s;
});
}
+ // check restrictions
+ if (o.restrictInput) {
+ t = txt;
+ for (i=0; i<s; i++){
+ if ($.inArray( t[i], o.acceptedKeys ) < 0) { txt = txt.replace(t[i], ''); }
+ }
+ }
// check max length too!
if (o.maxLength !== false && txt.length > o.maxLength) { txt = txt.substring(0, o.maxLength); }
return [ txt, txt.length - s ]; // return new text and change in length
@@ -579,6 +589,8 @@ $.widget('ui.keyboard', {
ui.layouts.custom = o.customLayout || { 'default' : ['{cancel}'] };
}
+ o.acceptedKeys = [];
+
// Main keyboard building loop
for ( set in ui.layouts[o.layout] ){
sets++;
@@ -671,6 +683,7 @@ $.widget('ui.keyboard', {
// Decimal - unique decimal point (num pad layout)
case 'dec':
+ o.acceptedKeys.push('.');
addKey('dec', 'dec', '.')
.appendTo(newRow);
break;
@@ -692,6 +705,7 @@ $.widget('ui.keyboard', {
// Change sign (for num pad layout)
case 'sign':
+ o.acceptedKeys.push('-');
addKey('sign', 'sign', function(){
if(/^\-?\d*\.?\d*$/.test( previewInput.val() )) {
previewInput.val( (previewInput.val() * -1) );
@@ -701,6 +715,7 @@ $.widget('ui.keyboard', {
break;
case 'space':
+ o.acceptedKeys.push(' ');
addKey('space', 'space', ' ').appendTo(newRow);
break;
@@ -714,6 +729,7 @@ $.widget('ui.keyboard', {
} else {
// regular button (not an action key)
+ o.acceptedKeys.push(keys[key]);
addKey(keys[key], keys[key], keys[key], true)
.attr('name','key_' + row + '_'+key)
.appendTo(newRow);
Oops, something went wrong.

0 comments on commit e015885

Please sign in to comment.