MediaWiki:DonationForm.js: Difference between revisions

Content deleted Content added
add validation of amounts when selected/entered
split getAmount and validateAmount out of validate method
Line 559:
 
var form = document.paypalcontribution; // we should really change this some day
 
if ( skipValidation || donationForm.validate() ) {
 
Line 582:
 
// Adyen override for cc
if ( paymentMethod === 'cc-adyen' ) {
paymentMethod = 'cc';
form.payment_method.value = 'cc';
Line 668:
};
 
/* Return amount selected or input */
/**
donationForm.validategetAmount = 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 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[i].checked ) {
$('.lp-haserror').removeClass('lp-haserror');
for ( var amounti = 0; i < form.amount[i].valuelength; i++ ) {
$('.lp-error').hide();
if ( form.amount[i].checked ) {
 
// Get amount selection= 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 !== '' ) {
var otherAmount = form.input_amount_other_box.value;
Line 697 ⟶ 689:
otherAmount = otherAmount.replace(/[\$£€¥,.]/g, '');
otherAmount = otherAmount.replace(/:/, '.');
form.amountGiven.value = otherAmount; // TODO: change this
amount = otherAmount;
}
 
if ( amount === null || isNaNparseFloat(amount) || amount <= 0 || amount < minAmount ) {;
 
$('.amount-options').addClass('lp-haserror');
if ( isNaN(amount) ) {
$('.lp-error-smallamount').show();
errorreturn = true0;
} else if ( amount > 10000 * minAmount ) {
$('.amount-options').addClass('lp-haserror');
$('.lp-error-bigamount').show();
error = true;
} else {
$('.return amount-options').removeClass('lp-haserror');
}
$('.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 ⟶ 729:
}
}
 
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 ⟶ 775:
// Block typing symbols in input field, otherwise Safari allows them and then chokes
// https://phabricator.wikimedia.org/T118741, https://phabricator.wikimedia.org/T173431
 
var amountOtherInput = document.getElementById('input_amount_other_box');
if ( amountOtherInput ) {
Line 749 ⟶ 781:
// Allow special keys in Firefox
if ((e.code == 'ArrowLeft') || (e.code == 'ArrowRight') ||
(e.code == 'ArrowUp') || (e.code == 'ArrowDown') ||
(e.code == 'Delete') || (e.code == 'Backspace')) {
return;
Line 760 ⟶ 792:
}
 
// Validate amountsamount when selected/entered
$('.amount-options').on( 'input change', donationForm.validatevalidateAmount );
 
// Disable submitting form with Enter key