Permalink
Browse files

Mask: Optional mask position with marked with a ?

  • Loading branch information...
1 parent 75ab04a commit 2414a943e33a5975c686e23e64fc5fb7730f7c78 @gnarf gnarf committed Aug 10, 2011
Showing with 37 additions and 8 deletions.
  1. +20 −3 tests/unit/mask/mask_events.js
  2. +17 −5 ui/jquery.ui.mask.js
@@ -4,10 +4,26 @@ module( "mask: events" );
/* TODO: Descide behavior of bluring non-valid inputs */
+test( "complete: Complete event fires when typing last non-optional character of mask", function() {
+ expect( 2 );
+ var input = $( "#mask1" ).val("").mask({
+ mask: "9?9",
+ complete: function( event, ui ) {
+ equal( this, input[0], "Complete event fired with correct context" );
+ }
+ }),
+ mask = input.data( "mask" );
+
+ input.focus();
+ mask._caret( 0 );
+ input.simulate( "keypress", { keyCode: "1".charCodeAt( 0 ) } );
+ equal( input.val(), "1_", "Value with optional character" );
+});
+
test( "complete: Complete event fires when typing last character of mask", function() {
expect( 3 );
var input = $( "#mask1" ).val("").mask({
- mask: "9",
+ mask: "99",
complete: function( event, ui ) {
equal( this, input[0], "Complete event fired with correct context" );
}
@@ -16,9 +32,10 @@ test( "complete: Complete event fires when typing last character of mask", funct
input.focus();
mask._caret( 0 );
- deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct" );
input.simulate( "keypress", { keyCode: "1".charCodeAt( 0 ) } );
- equal( input.val(), "1", "Value is valid" );
+ equal( input.mask( "valid" ), false, "Mask is not yet valid" );
+ input.simulate( "keypress", { keyCode: "2".charCodeAt( 0 ) } );
+ equal( input.val(), "12", "Value with optional character" );
});
asyncTest( "focus: Initial Caret Positioning", function() {
View
@@ -100,15 +100,25 @@ $.widget( "ui.mask", {
}
},
_parseMask: function() {
- var key, x, bufferObject,
+ var key, x, bufferObject, originalPosition,
index = -1,
options = this.options,
- mask = options.mask ;
+ mask = options.mask;
this.buffer = [];
if ( !mask ) {
return;
}
+
+ optionalPosition = mask.indexOf( "?" );
+ if ( optionalPosition === -1 ) {
+ this.optionalPosition = mask.length;
+ } else {
+ this.optionalPosition = optionalPosition;
+
+ // remove the ? from the mask
+ mask = mask.substr( 0, optionalPosition ) + mask.substr( optionalPosition + 1 );
+ }
// search for definied "masks"
for ( key in options.definitions ) {
while ( ( index = mask.indexOf( key, index + 1 ) ) > -1 ) {
@@ -367,11 +377,13 @@ $.widget( "ui.mask", {
value += bufferObject.value;
} else if ( !raw ) {
value += this.options.placeholder;
- this.isValid = false;
+ if ( bufferPosition < this.optionalPosition ) {
+ this.isValid = false;
+ }
}
}
- if ( !wasValid && this.isValid ) {
- this._trigger( "complete", this.currentEvent );
+ if ( this.currentEvent && !wasValid && this.isValid ) {
+ this._trigger( "complete", this.currentEvent, {} );
}
return value;
},

0 comments on commit 2414a94

Please sign in to comment.