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 */
/**▼
* Validate the form. ▼
var form = document.paypalcontribution;▼
*/▼
▲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▼
$('.lp-haserror').removeClass('lp-haserror');▼
$('.lp-error').hide();▼
if ( form.amount[i].checked ) {
▲ 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;
}
$('.amount-options').addClass('lp-haserror');▼
if ( isNaN(amount) ) {
$('.lp-error-smallamount').show();▼
} else if ( amount > 10000 * minAmount ) {▼
$('.amount-options').addClass('lp-haserror');▼
$('.lp-error-bigamount').show();▼
error = true;▼
} else {
▲ }
$('.lp-error-smallamount, .lp-error-bigamount').hide();▼
}
▲/**
▲ */
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 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;
$('.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
$('.amount-options').on( 'input change', donationForm.
// Disable submitting form with Enter key
|