Michael Harmon
Michael Harmon

Reputation: 43

Remove "$" from form input field

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

Answers (5)

Marco Johannesen
Marco Johannesen

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

millimoose
millimoose

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

McAden
McAden

Reputation: 13972

on submission, use the "replace" method on the contents of the field before returning true.

Upvotes: 0

Mike Thomsen
Mike Thomsen

Reputation: 37506

$('form').submit(function() {

    $('input[type=text]', this).each(function() {
        $(this).val( $(this).val().replace(/\$/g, '') );
    });

    return true;
});

Upvotes: 2

Marek Sebera
Marek Sebera

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

Related Questions