简体   繁体   中英

Remove Dollar Sign and Decimal Point from Input Post

I have a php form that is for processing payments and I am using js to add a dollar sign and decimal point to the input field. I can't seem to find out how I can then remove the dollar sign and decimal point on submit.

$amount = trim($_POST['amount']);

<div class="input-block">
<label for="amount" class="label_comment"><strong>Donation Amount</strong>*</label>
<input type="text" name="amount" value="<?php echo $_POST['amount']; ?>" placeholder="$" data-stripe="amount" id="amount" required="">
</div>

<script type="text/javascript" src="/js/jquery.price_format.2.0.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#amount').priceFormat({
prefix: '$',
centsSeparator: '.',
thousandsSeparator: ','
});
});

</script>

You can either use the .unmask() function to get the raw value and set in on a hidden input OR reset your formatting in the onSubmit event .

<form id="yourForm">
  <div class="input-block">
  <label for="amount" class="label_comment"><strong>Donation Amount</strong>*</label>
  <input type="text" name="amount" value="<?php echo $_POST['amount']; ?>" placeholder="$" data-stripe="amount" id="amount" required="">

  <input type="hidden" id="rawAmountField" name="rawAmount" value="" />
</div>
</form>

Two changes: a hidden field and the #id argument for the -tag. Then you can add like

<script type="text/javascript">
$(document).ready(function() {
  $('#yourForm').submit(function( event ) {

    /* Set the hidden field */
    $('#rawAmountField').value($('#amount).unmask());

    /* OR reset format like */
    $('#amount').priceFormat({
      prefix: '',
      centsSeparator: '.',
      thousandsSeparator: ''
    });
  });
});
</script>

I think the best way to handle this would be to use the filter_input() function. It would work like:

$filtered_amount = filter_input(INPUT_POST, 'amount', FILTER_SANITIZE_NUMBER_INT);

That would strip everything from $_POST['amount'] that isn't a numerical digit. Of course it requires the use of PHP5.2 or greater.

function remove(number) { 

    var tag = number.charAt(0);
    if(tag=='$')
    {
        var res = number.slice(1);
    }
    if(tag!='$')
    {
        var res = number.slice(0);
    }
    document.formname.roundedfield.value = '$'+ parseInt(res); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM