Skip to content

Commit

Permalink
Added manual state check to verify if data was changed since control …
Browse files Browse the repository at this point in the history
…got focus. If changed, the respective event is invoked, emulating the default browser action
  • Loading branch information
pbalduino committed Aug 4, 2011
1 parent af8cc26 commit 6c6b5be
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions jquery.maskMoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,43 @@

return this.each(function() {
var input = $(this);
var dirty = false;

function markAsDirty() {
if(!dirty) {
console.log('gross...');
}
dirty = true;
}

function clearDirt(){
if(dirty){
console.log('aaahhh...');
}
dirty = false;
}

function keypressEvent(e) {
e = e||window.event;
var k = e.charCode||e.keyCode||e.which;
if (k == undefined) return false; //needed to handle an IE "special" event
if (input.attr('readonly') && (k!=13&&k!=9)) return false; // don't allow editing of readonly fields but allow tab/enter

console.log('key', k);
if (k<48||k>57) { // any key except the numbers 0-9
if (k==45) { // -(minus) key
markAsDirty();
input.val(changeSign(input));
return false;
}
if (k==43) { // +(plus) key
} else if (k==43) { // +(plus) key
markAsDirty();
input.val(input.val().replace('-',''));
return false;
} else if (k==13||k==9) { // enter key or tab key
if(dirty){
clearDirt();
$(this).change();
}
return true;
} else if (k==37||k==39) { // left arrow key or right arrow key
return true;
Expand All @@ -79,6 +100,7 @@
var endPos = selection.end;
x.value = x.value.substring(0, startPos) + key + x.value.substring(endPos, x.value.length);
maskAndPosition(x, startPos + 1);
markAsDirty();
return false;
}
}
Expand Down Expand Up @@ -106,8 +128,14 @@
x.value = x.value.substring(0, startPos) + x.value.substring(endPos, x.value.length);
}
maskAndPosition(x, startPos);
maskAsDirty();
return false;
} else if (k==9) { // tab key
console.log('so...', dirty);
if(dirty) {
$(this).change();
clearDirt();
}
return true;
} else if (k==46||k==63272) { // delete key (with special case for safari)
preventDefault(e);
Expand All @@ -119,6 +147,7 @@
x.value = x.value.substring(0, startPos) + x.value.substring(endPos, x.value.length);
}
maskAndPosition(x, startPos);
markAsDirty();
return false;
} else { // any other key
return true;
Expand Down Expand Up @@ -330,4 +359,4 @@
end: end
};
}
})(jQuery);
})(jQuery);

0 comments on commit 6c6b5be

Please sign in to comment.