JSFiddle - React, Tailwind, and code Playground

by Marcus Ekwall

HTML

<form id="form_paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="business" value="mail@url.url"/>
    <input type="hidden" name="cmd" value="_xclick"/>
    <input type="hidden" name="return" value="http://returnpage.url"/>
    <input type="hidden" name="cancel_return" value="http://cancelreturnpage.url"/>
    <input type="hidden" name="rm" value="2"/>
    <input type="hidden" name="currency_code" value="USD"/>
    <input type="hidden" name="quantity" value="1"/>
    <input type="hidden" name="item_name" value="Item Name"/>
    <input type="hidden" name="item_number" value="Item ID"/>
    
    <label>Amount (US$) : </label>
    <input type="text" name="amount" id="input_amount" class="text" />
    
    <input type="hidden" name="invoice" value="Post ID"/>
    <input type="hidden" name="cbt" value="Make Payment &rarr;"/>
    <input type="hidden" name="item_number" value="Item ID"/>
    <input type="submit" name="submit" value="Confirm & Pay &rarr;" class="submit" />
    <br/>
    <span id="msg" style="display:none;"></span>
    
    <p id="site_commission_box" style="display: none;">-2% of the amount it will be <b>$<span class="text_decoration"></span> USD</b>.</p>
</form>

CSS

.red {color:#ff0000;}
.icon_warning {background:transparent url(exclamation.png) left no-repeat;padding:4px;padding-left:20px;}
form#form_paypal input {padding:3px;border:1px solid #ddd;background:#fefefe;}
form#form_paypal input#input_amount {width:50px;}
form#form_paypal .submit {cursor:pointer;border-style:outset;}

JavaScript

$(function() {
    // the minimum required value to be entered.
    // in this case PayPal takes $0.35 from a $1
    // donation, hence we ask for at least $1.35
    var minimum_value = 1.35;
    
    // cache elements that are used at least twice
    var $amount = $("#input_amount"),
        $msg = $("#msg"),
        $commission = $("#site_commission_box");
    
    // attach handler to input keydown event
    $amount.keyup(function(e){
        if (e.which == 13) {
            return;
        }
        var amount = parseFloat($amount.val()),
            commission = amount*0.02;
        
        if (isNaN(commission) || isNaN(amount)) {
            $msg.hide();
            $commission.hide();
            return;
        }
        
        if (amount <= minimum_value) {
            $commission.hide();
            $msg
                .text("PayPal takes $0.35 commission for a $"+amount+" donation.")
                .fadeIn();
        } else {
            $msg.hide();
            $commission
                .fadeIn()
                .find("span")
                    .text((amount-commission).toFixed(2));
        }
    });
    
    // attach handler to the form submit event
    $('#form_paypal').submit(function() {
        // check if there is an amount entered
        if ($amount.val() > null) {
            // is the amount equal to or higher than the minimum_value?
            if ($amount.val() < minimum_value) {
                // need more amount
                // show more amount error
                $msg
                    .addClass("icon_warning_red")
                    .text("Please enter an amount and try again.")
                    .fadeIn();
                return false; // prevent the form from submitting
            }
            else {
                // amount is more than minimum_value
                // show activity
                $msg
                    .removeClass("icon_warning_red")
                    .html('<img src="loader.gif"...