Browse Source

fix isValid fn for optional masks => first tests ok ;-)

Robin Herbots 13 years ago
parent
commit
d381bcdd35
1 changed files with 6 additions and 8 deletions
  1. 6 8
      js/jquery.inputmask.js

+ 6 - 8
js/jquery.inputmask.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2012 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 2.0.0e
+* Version: 2.0.0f
 */
 
 (function ($) {
@@ -260,7 +260,7 @@
                 });
             }
 
-            function generateMaskSets() {  //TODO FIXME generate masksets
+            function generateMaskSets() {  //TODO improve generate masksets
             	var ms = [];
             	
             	function generateMask(maskPrefix, maskPart){
@@ -280,7 +280,7 @@
                     	  		  "tests": getTestingChain(newMask),
                     	  		  "lastValidPosition": 0
                 		});	                	                	
-            			if(maskParts.length > 1 && maskParts[1].length > 0) {
+            			if(maskParts.length > 1 && maskParts[1].split(opts.optionalmarker.start).length > 1) {
             				generateMask(maskPrefix + masks[0] + masks[1], maskParts[1]);
             				generateMask(maskPrefix + masks[0], maskParts[1]);
             			}
@@ -313,7 +313,7 @@
             }
 
             function isValid(pos, c, buffer, strict, isRTL) { //strict true ~ no correction or autofill
-            	var results = [], result = false;
+            	var results = [], result = false, currentActiveMasksetIndex = activeMasksetIndex;
             	$.each(masksets, function(index, value) {          	
                 	var activeMaskset = this;
                 	activeMasksetIndex = index;
@@ -329,15 +329,15 @@
                     		}
                     		//return is false or a json object => { pos: ??, c: ??}
                     		results[index] = activeMaskset['tests'][testPos].fn != null ? activeMaskset['tests'][testPos].fn.test(chrs, buffer, pos, strict, opts) : false;
-							//trace(results[index]);
 							if(results[index] !== false) {
 							 	if (results[index] !== true)
                                		activeMaskset['lastValidPosition'] = results[index].pos || pos; //set new position from isValid
                             	else activeMaskset['lastValidPosition'] = pos;
-							}   	
+							} else activeMaskset['lastValidPosition'] = isRTL ? seekNext(buffer, pos) : seekPrevious(buffer, pos); //autocorrect validposition from backspace etc  	
                 		}
                 	}
                 });
+                activeMasksetIndex = currentActiveMasksetIndex; //reset activeMasksetIndex
                 $.each(masksets, function(index, value) {
                 	var activeMaskset = this;
                 	if(isRTL ? activeMaskset['lastValidPosition'] <= pos : activeMaskset['lastValidPosition'] >= pos) {
@@ -347,8 +347,6 @@
                 	}
                 });
                 
-                //trace(activeMasksetIndex);
-                
                 setTimeout(opts.onKeyValidation.call(this, result, opts), 0); //extra stuff to execute on keydown
                 return result;
             }