Skip to content

Instantly share code, notes, and snippets.

@sbussard
Created August 19, 2011 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbussard/1157529 to your computer and use it in GitHub Desktop.
Save sbussard/1157529 to your computer and use it in GitHub Desktop.
Price validation using jQuery
// usage: $("#price").priceField();
$.fn.getCaret = function() { // adapted from http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea
var ctrl = this[0];
var CaretPos = 0; // IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -ctrl.value.length);
CaretPos = Sel.text.length;
} else if (ctrl.selectionStart || ctrl.selectionStart == '0') { // Firefox support
CaretPos = ctrl.selectionStart;
}
return (CaretPos);
};
$.fn.priceField = function() {
$(this).keydown(function(e){
var val = $(this).val();
var code = (e.keyCode ? e.keyCode : e.which);
var nums = ((code >= 96) && (code <= 105)) || ((code >= 48) && (code <= 57)); //keypad || regular
var backspace = (code == 8);
var specialkey = (e.metaKey || e.altKey || e.shiftKey);
var arrowkey = ((code >= 37) && (code <= 40));
var Fkey = ((code >= 112) && (code <= 123));
var decimal = ((code == 110 || code == 190) && val.indexOf('.') == -1);
// UGLY!!
var misckey = (code == 9) || (code == 144) || (code == 145) || (code == 45) || (code == 46) || (code == 33) || (code == 34) || (code == 35) || (code == 36) || (code == 19) || (code == 20) || (code == 92) || (code == 93) || (code == 27);
var properKey = (nums || decimal || backspace || specialkey || arrowkey || Fkey || misckey);
var properFormatting = backspace || specialkey || arrowkey || Fkey || misckey || ((val.indexOf('.') == -1) || (val.length - val.indexOf('.') < 3) || ($(this).getCaret() < val.length - 2));
if(!(properKey && properFormatting)) {
return false;
}
});
$(this).blur(function(){
var val = $(this).val();
if(val === '') {
$(this).val('0.00');
} else if(val.indexOf('.') == -1) {
$(this).val(val + '.00');
} else if(val.length - val.indexOf('.') == 1) {
$(this).val(val + '00');
} else if(val.length - val.indexOf('.') == 2) {
$(this).val(val + '0');
}
});
return $(this);
};
@sbussard
Copy link
Author

Great suggestion!

I jshinted his pattern: http://jsbin.com/eboveq/edit#source
but I don't know if it will work this way, I just didn't like his style.

I might make this into a single plugin when I get time

@Paden
Copy link

Paden commented Jun 22, 2012

Well, the thing about jQuery plugins is that you normally don't want return a value; you return the object. Otherwise chaining is broken,

@sbussard
Copy link
Author

I'm not actually returning a value, that was just a poor choice for a variable name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment