2

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.

  • 1
    How is the data submitted? In any case, have a look at [Remove characters from a string](http://stackoverflow.com/questions/4846978/remove-characters-from-a-string) – Felix Kling Oct 21 '11 at 18:01
  • 4
    Use `.replace(/[^\d.]/g,"")`. This function removes each character, except for digits and dots. – Rob W Oct 21 '11 at 18:04

5 Answers5

3
$('#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 :)

Marco Johannesen
  • 12,904
  • 6
  • 29
  • 36
2
$('form').submit(function() {

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

    return true;
});
Mike Thomsen
  • 35,490
  • 10
  • 55
  • 80
  • Mike, tried your method -- do I need to change 'form' to the ID of the form? Also -- would I change input[type-text] to input[name='chargetotal'] ? doesn't seem to be working – Michael Harmon Oct 21 '11 at 19:01
  • Yes. You'd want to change form to a selector appropriate for the form you are targeting. Also, the reason I did type=text is to have it go against all text fields. Don't add quote marks in selector. – Mike Thomsen Oct 21 '11 at 22:00
1

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); 
Marek Sebera
  • 38,635
  • 35
  • 154
  • 241
0

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

McAden
  • 13,322
  • 5
  • 36
  • 63
0

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.

millimoose
  • 37,888
  • 9
  • 76
  • 132