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 */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
* TODO: Clean up to be more like banners i.e. split out getAmount |
|||
⚫ | |||
⚫ | |||
var amount = null; |
var amount = null; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// If there are some amount radio buttons, then look for the checked one |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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; |
||
} |
} |
||
amount = parseFloat(amount); |
|||
⚫ | |||
if ( isNaN(amount) ) { |
|||
⚫ | |||
return 0; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} else { |
} else { |
||
return amount; |
|||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
donationForm.validate = function() { |
|||
⚫ | |||
var form = document.paypalcontribution; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if ( !frb.validateAmount() ) { |
|||
⚫ | |||
} |
} |
||
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(); |
|||
⚫ | |||
⚫ | |||
if ( amount === null || isNaN(amount) || amount <= 0 || amount < minAmount ) { |
|||
⚫ | |||
⚫ | |||
return false; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return false; |
|||
⚫ | |||
$('.amount-options').removeClass('lp-haserror'); |
|||
⚫ | |||
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 |
// Validate amount when selected/entered |
||
$('.amount-options').on( 'input change', donationForm. |
$('.amount-options').on( 'input change', donationForm.validateAmount ); |
||
// Disable submitting form with Enter key |
// Disable submitting form with Enter key |