MediaWiki:DonationForm.js: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
add validation of amounts when selected/entered
split getAmount and validateAmount out of validate method
Line 559: Line 559:


var form = document.paypalcontribution; // we should really change this some day
var form = document.paypalcontribution; // we should really change this some day

if ( skipValidation || donationForm.validate() ) {
if ( skipValidation || donationForm.validate() ) {


Line 582: Line 582:


// Adyen override for cc
// Adyen override for cc
if( paymentMethod === 'cc-adyen' ) {
if ( paymentMethod === 'cc-adyen' ) {
paymentMethod = 'cc';
paymentMethod = 'cc';
form.payment_method.value = 'cc';
form.payment_method.value = 'cc';
Line 668: Line 668:
};
};


/* Return amount selected or input */
/**
donationForm.getAmount = function() {
* Validate the form.
var form = document.paypalcontribution;
* TODO: Clean up to be more like banners i.e. split out getAmount
*/
donationForm.validate = function() {

var amount = null;
var amount = null;
var error = false;
var form = document.paypalcontribution;
var currency = document.paypalcontribution.currency_code.value;
var minAmount = donationForm.minimums[ currency ] || 1;


// If there are some amount radio buttons, then look for the checked one
// Reset all errors
if ( form.amount ) {
$('.lp-haserror').removeClass('lp-haserror');
for ( var i = 0; i < form.amount.length; i++ ) {
$('.lp-error').hide();
if ( form.amount[i].checked ) {

// Get amount selection
amount = form.amount[i].value;
for ( var i = 0; i < form.amount.length; i++ ) {
}
if ( form.amount[i].checked ) {
amount = form.amount[i].value;
}
}
}
}


// Check the "other" amount box
if ( form.input_amount_other_box.value !== '' ) {
if ( form.input_amount_other_box.value !== '' ) {
var otherAmount = form.input_amount_other_box.value;
var otherAmount = form.input_amount_other_box.value;
Line 697: Line 689:
otherAmount = otherAmount.replace(/[\$£€¥,.]/g, '');
otherAmount = otherAmount.replace(/[\$£€¥,.]/g, '');
otherAmount = otherAmount.replace(/:/, '.');
otherAmount = otherAmount.replace(/:/, '.');
form.amountGiven.value = otherAmount;
form.amountGiven.value = otherAmount; // TODO: change this
amount = otherAmount;
amount = otherAmount;
}
}


if ( amount === null || isNaN(amount) || amount <= 0 || amount < minAmount ) {
amount = parseFloat(amount);

$('.amount-options').addClass('lp-haserror');
if ( isNaN(amount) ) {
$('.lp-error-smallamount').show();
error = true;
return 0;
} else if ( amount > 10000 * minAmount ) {
$('.amount-options').addClass('lp-haserror');
$('.lp-error-bigamount').show();
error = true;
} else {
} else {
$('.amount-options').removeClass('lp-haserror');
return amount;
}
$('.lp-error-smallamount, .lp-error-bigamount').hide();

}


/**
* Validate the form.
*/
donationForm.validate = function() {

var error = false;
var form = document.paypalcontribution;

// Reset all errors
$('.lp-haserror').removeClass('lp-haserror');
$('.lp-error').hide();

if ( !frb.validateAmount() ) {
error = true;
}
}


Line 723: Line 729:
}
}
}
}

return !error;
return !error;
}

/**
* Check if selected amount is valid i.e. a positive number, between minimum and maximum.
* If not, show an error and return false.
*/
donationForm.validateAmount = function() {

var amount = donationForm.getAmount();
var currency = document.paypalcontribution.currency_code.value;
var minAmount = donationForm.minimums[ currency ] || 1;

if ( amount === null || isNaN(amount) || amount <= 0 || amount < minAmount ) {
$('.amount-options').addClass('lp-haserror');
$('.lp-error-smallamount').show();
return false;
} else if ( amount > 10000 * minAmount ) {
$('.amount-options').addClass('lp-haserror');
$('.lp-error-bigamount').show();
return false;
} else {
$('.amount-options').removeClass('lp-haserror');
$('.lp-error-smallamount, .lp-error-bigamount').hide();
return true;
}

}
}


Line 743: Line 775:
// Block typing symbols in input field, otherwise Safari allows them and then chokes
// Block typing symbols in input field, otherwise Safari allows them and then chokes
// https://phabricator.wikimedia.org/T118741, https://phabricator.wikimedia.org/T173431
// https://phabricator.wikimedia.org/T118741, https://phabricator.wikimedia.org/T173431

var amountOtherInput = document.getElementById('input_amount_other_box');
var amountOtherInput = document.getElementById('input_amount_other_box');
if ( amountOtherInput ) {
if ( amountOtherInput ) {
Line 749: Line 781:
// Allow special keys in Firefox
// Allow special keys in Firefox
if ((e.code == 'ArrowLeft') || (e.code == 'ArrowRight') ||
if ((e.code == 'ArrowLeft') || (e.code == 'ArrowRight') ||
(e.code == 'ArrowUp') || (e.code == 'ArrowDown') ||
(e.code == 'ArrowUp') || (e.code == 'ArrowDown') ||
(e.code == 'Delete') || (e.code == 'Backspace')) {
(e.code == 'Delete') || (e.code == 'Backspace')) {
return;
return;
Line 760: Line 792:
}
}


// Validate amounts when selected/entered
// Validate amount when selected/entered
$('.amount-options').on( 'input change', donationForm.validate );
$('.amount-options').on( 'input change', donationForm.validateAmount );


// Disable submitting form with Enter key
// Disable submitting form with Enter key