Skip to content
Browse files

Mask: adding a value method to get and set raw value without mask lit…

…erals / placeholders
  • Loading branch information...
1 parent 719aae5 commit 9d7ea7973b5bb8d3b008bbce70c669c00439095f @gnarf gnarf committed
Showing with 31 additions and 6 deletions.
  1. +2 −2 tests/unit/mask/mask_core.js
  2. +12 −0 tests/unit/mask/mask_methods.js
  3. +17 −4 ui/jquery.ui.mask.js
View
4 tests/unit/mask/mask_core.js
@@ -120,11 +120,11 @@ test( "Parsing initial value skips literals", function() {
input.val("123456").mask({
mask: "99/99/99"
});
-
+
equal( input.val(), "12/34/56", "Literals were inserted into val");
input.mask( "option", "mask", "99-99-99" );
equal( input.val(), "12-34-56", "Old literals were ignored, and new ones inserted into val");
-
+
});
}( jQuery ) );
View
12 tests/unit/mask/mask_methods.js
@@ -13,4 +13,16 @@ test( "refresh", function() {
equal( input.val(), "12/34/56", "Refresh re-parsed the value of the input" );
});
+test( "value: able to get (and set) raw values", function() {
+ expect( 3 );
+ var input = $( "#mask1" ).mask({
+ mask: "99/99/99"
+ });
+
+ equal( input.mask( "value" ), "", "Reading empty raw value" );
+ input.mask( "value", "123456" );
+ equal( input.val(), "12/34/56", "Raw value set properly" );
+ equal( input.mask( "value" ), "123456", "Raw value read correctly" );
+});
+
}( jQuery ) );
View
21 ui/jquery.ui.mask.js
@@ -36,6 +36,16 @@ $.widget( "ui.mask", {
this._paint();
},
+ // returns (or sets) the value without the mask
+ value: function( value ) {
+ if ( value !== undefined ) {
+ this.element.val( value );
+ this.refresh();
+ } else {
+ return this._getValue( true );
+ }
+ },
+
_setOption: function( key, value ) {
this._super( "_setOption", key, value );
if ( key === "mask" ) {
@@ -329,7 +339,7 @@ $.widget( "ui.mask", {
}
return lastFilledPosition;
},
- _paint: function() {
+ _getValue: function( raw ) {
var bufferPosition,
bufferObject,
bufferLength = this.buffer.length,
@@ -337,15 +347,18 @@ $.widget( "ui.mask", {
for ( bufferPosition = 0; bufferPosition < bufferLength; bufferPosition += bufferObject.length ) {
bufferObject = this.buffer[ bufferPosition ];
- if ( bufferObject.literal ) {
+ if ( bufferObject.literal && !raw ) {
value += bufferObject.literal;
} else if ( bufferObject.value ) {
value += bufferObject.value;
- } else {
+ } else if ( !raw ) {
value += this.options.placeholder;
}
}
- this.element.val( value );
+ return value;
+ },
+ _paint: function() {
+ this.element.val( this._getValue() );
},
// returns the value if valid, otherwise returns false

0 comments on commit 9d7ea79

Please sign in to comment.
Something went wrong with that request. Please try again.