Skip to content

Instantly share code, notes, and snippets.

@devth
Created October 14, 2010 20:10
Show Gist options
  • Save devth/626947 to your computer and use it in GitHub Desktop.
Save devth/626947 to your computer and use it in GitHub Desktop.
$("input.dollars, input.number, input.percentage").blur(function(){
// VALIDATE
var input = $(this);
if (input.val() == "") input.val("0");
if (input.attr("data-min") != undefined && input.attr("data-max") != undefined){
var min = parseInt(input.attr("data-min"));
var max = parseInt(input.attr("data-max"));
var val = parseInt(input.val());
if ( val < min || val > max ){
input.addClass('error');
var minmax = { min:min, max:max };
var warning = input.closest('.formRow').find('.warning');
var errors = {
percentage: "Value must be between {min} and {max}%",
dollars: "Value must be between ${min} and ${max}",
number: "Value must be between {min} and {max}"
};
warning.text( errors[input.attr("data-type")].supplant(minmax) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment