Skip to content

Instantly share code, notes, and snippets.

@rickycheers
Last active December 18, 2015 11:49
Show Gist options
  • Save rickycheers/5778259 to your computer and use it in GitHub Desktop.
Save rickycheers/5778259 to your computer and use it in GitHub Desktop.
Minimum Amount Validator for 2Dialog Forms
var MinimumAmountValidator = Class.create({
settings: {
"minimum_amount": 1,
"checkbox_id" : null,
"input_id" : null,
"only_integer" : false
},
initialize: function( settings ){
Object.extend(this.settings, settings);
this.$free_donation_checkbox = $(this.settings.checkbox_id);
this.$free_donation_text_input = $(this.settings.input_id);
this.$free_donation_label = this.$free_donation_text_input.next();
this.listenEvents();
},
listenEvents: function(){
var self = this;
$$('input[type="radio"]').each(function(radio){
radio.observe('click', self.validateIfChecked.bind(self));
});
this.$free_donation_text_input.observe('focus', self.validateIfChecked.bind(self));
this.$free_donation_text_input.observe('change', self.validateAmount.bind(self));
},
validateIfChecked: function(){
if( this.$free_donation_checkbox.checked ){
$$('input[id*=submitButton_]')[0].hide();
this.validateAmount();
} else {
this.removeErrorMessage();
$$('input[id*=submitButton_]')[0].show();
}
},
validateAmount: function(){
var value = this.$free_donation_text_input.getValue();
var minimum_amount = this.settings.minimum_amount;
if( this.settings.only_integer ){
value = this.integerize( value );
// update the input value, but don't fire the change event...
this.$free_donation_text_input.value = value;
}
this.removeErrorMessage();
this.$free_donation_text_input.removeClassName('form-error');
if( value < minimum_amount ){
this.insertErrorMessages();
this.$free_donation_text_input.addClassName('form-error');
$$('input[id*=submitButton_]')[0].hide();
} else {
$$('input[id*=submitButton_]')[0].show();
}
},
integerize: function( amount ){
amount = amount.replace(',', '.') * 1;
amount = Math.round(amount);
return amount;
},
insertErrorMessages: function(){
var message = '<span id="minimum_amount_error" style="color:red; margin-left: 10px; display: block;">The minimum amount is $'+this.settings.minimum_amount+'</span>';
this.$free_donation_checkbox.ancestors()[0].insert(message);
var submit_button_error_message = '<span id="minimum_amount_submit_button_error" style="color:red; margin-left: 10px">We\'re sorry. The minimum online donation amount is $'+this.settings.minimum_amount+'. Thank you.</span>';
$$('input[id*=submitButton_]')[0].ancestors()[0].insert(submit_button_error_message);
},
removeErrorMessage: function(){
if( $('minimum_amount_error') ){
$('minimum_amount_error').remove();
}
if( $('minimum_amount_submit_button_error') ){
$('minimum_amount_submit_button_error').remove();
}
}
});
/* Update the following settings with the correct values */
new MinimumAmountValidator({
minimum_amount: 12, // update this
checkbox_id : "frmItem_158_963", // update this
input_id : "donation_value_198_963", // update this
only_integer : true // update here, if not, default is false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment