Reputation: 43
I am working on a site that is using a form to accept donations for their foundation. The payment gateway returns an error if a "$" is included with the actual dollar amount.
Is there an easy way to strip the "$" out of the input field before submitting the form if they type one in?
Thanks.
Upvotes: 2
Views: 3689
Reputation: 13134
$('#donationform').submit(function() {
//get amount value
var CurrencyField = $("input[name='chargetotal']").val()
//return amount without $
$("input[name='chargetotal']").val( CurrencyField.replace('$', '') );
});
Should do the trick :)
Upvotes: 3
Reputation: 39950
You can use String.replace
to remove any substring from a string:
var amount = $('#amount').val().replace('$', '');
will make amount
contain the value of the form field with id="amount"
without the first dollar sign in it.
Upvotes: 0
Reputation: 13972
on submission, use the "replace" method on the contents of the field before returning true.
Upvotes: 0
Reputation: 37506
$('form').submit(function() {
$('input[type=text]', this).each(function() {
$(this).val( $(this).val().replace(/\$/g, '') );
});
return true;
});
Upvotes: 2
Reputation: 40651
on client-side (jquery/javascript)
var fieldvalue = {...get from input field...};
fieldvalue = fieldvalue.replace(/\$/,"");
on server-side (php, after submitting)
$fieldvalue = $_POST['fieldname'];
$fieldvalue = str_replace("$", "", $fieldvalue);
Upvotes: 1