Template:Validation.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
use inline error for optin
rm auto focusing of Other on amount error, it's probably more confusing than helpful
Line 50: Line 50:
}
}
if ( error ) {
if ( error ) {
$( "#input_amount_other" ).focus();
$( "#input_amount_other_box" ).focus();
alert( "</html>{{int:donate_interface-amount-error}}<html>" );
alert( "</html>{{int:donate_interface-amount-error}}<html>" );
} else if ( amount < minimums[currency] ) {
} else if ( amount < minimums[currency] ) {
$( "#input_amount_other_box" ).val( '' );
$( "#input_amount_other_box" ).val( '' );
$( "#input_amount_other" ).focus();
$( "#input_amount_other_box" ).focus();
alert( "</html>{{int:donate_interface-smallamount-error}}<html>".replace('$1', (minimums[currency] + ' ' + currency) ) );
alert( "</html>{{int:donate_interface-smallamount-error}}<html>".replace('$1', (minimums[currency] + ' ' + currency) ) );
error = true;
error = true;
Line 63: Line 59:
if ( amount > 10000*minimums[currency]) {
if ( amount > 10000*minimums[currency]) {
$( "#input_amount_other_box" ).val( '' );
$( "#input_amount_other_box" ).val( '' );
$( "#input_amount_other" ).focus();
$( "#input_amount_other_box" ).focus();
errorMessageLarge = "</html>{{int:Donate interface-bigamount-error}}<html>";
errorMessageLarge = "</html>{{int:Donate interface-bigamount-error}}<html>";
errorMessageLarge = errorMessageLarge.replace('$1',10000*minimums[currency]);
errorMessageLarge = errorMessageLarge.replace('$1',10000*minimums[currency]);

Revision as of 18:19, 5 October 2018

<html>
<script>
//Should this be here? 
var amount = null;

function DefaultSubmit(formfield,Action) {
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (Action) keycode = Action.which;
  else return true;

  if (keycode == 13) {
    redirectPayment(</html>{{#ifeq:block|block|'1'|'2'}}<html>);
    return false;
  }
  else
    return true;
}

function validateForm(form){

  var minimums = {</html>{{2012FR/Switch/Currency/Minimums}}<html>};

  var error = true;

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

  if ( form.input_amount_other_box.value != "" ) {
    var otherAmount = form.input_amount_other_box.value;
    otherAmount = otherAmount.replace(/[,.](\d)$/, '\:$10');
    otherAmount = otherAmount.replace(/[,.](\d)(\d)$/, '\:$1$2');
    otherAmount = otherAmount.replace(/[\$£€¥,.]/g, '');
    otherAmount = otherAmount.replace(/:/, '.');
    form.input_amount_other_box.value = otherAmount;
    form.amountGiven.value = otherAmount;
    amount = otherAmount;
  }

  // Check amount is a real number
  error = ( amount == null || isNaN( amount ) || amount.value <= 0 );
  // Check amount is at least the minimum
  var currency = form.currency_code.value;
  if ( typeof( minimums[currency] ) == 'undefined' ) {
    minimums[currency] = 1;
  }
  if ( error ) {
    alert( "</html>{{int:donate_interface-amount-error}}<html>" );
  } else if ( amount < minimums[currency] ) {
    $( "#input_amount_other_box" ).val( '' );
    alert( "</html>{{int:donate_interface-smallamount-error}}<html>".replace('$1', (minimums[currency] + ' ' + currency) ) );
    error = true;
  }
  
  if ( amount > 10000*minimums[currency]) {
    $( "#input_amount_other_box" ).val( '' );
    errorMessageLarge = "</html>{{int:Donate interface-bigamount-error}}<html>";
    errorMessageLarge = errorMessageLarge.replace('$1',10000*minimums[currency]);
    errorMessageLarge = errorMessageLarge.replace('$2',currency);
    errorMessageLarge = errorMessageLarge.replace('$3','benefactors@wikimedia.org');
    alert(errorMessageLarge);
    error = true;
  }

  if ( form.opt_in ) {
    if ( $("input[name='opt_in']:checked").val() === undefined ) {
      $('#error-optin').show();
      error = true;
    } else {
      $('#error-optin').hide();
      form.variant.value = 'emailExplain';
    }
  }

  return !error;
}
</script>
</html>