2

I found this plugin for jQuery where you can type a number (let's say "650") and the input field would automatically change it to €650,-.

But when I submit the form, the form value is "€650,-" (not very suprising).

What I want is the functionality that the plugin would change it back to "650" on submitting the form. The inputs should only accept whole euro's, so "650,40" is not permitted.

Does anyone know how to accomplish this?

ssilas777
  • 9,672
  • 4
  • 45
  • 68
HansElsen
  • 1,639
  • 5
  • 27
  • 47

4 Answers4

2

This is what I used to answer this question:

$('form').submit(function(){
    var form = $(this);
    $('input').each(function(i){
        var self = $(this);
        try{
            var v = self.autoNumeric('get');
            self.autoNumeric('destroy');
            self.val(v);
        }catch(err){
            console.log("Not an autonumeric field: " + self.attr("name"));
        }
    });
    return true;
});

Thanks to:

autoNumeric.js remove formatting before submitting form

Community
  • 1
  • 1
HansElsen
  • 1,639
  • 5
  • 27
  • 47
1

You have to submit the form manually, for example if you have form like this...

<form name="f1">
<input type="text" id="autoNum"/>
<input type="button" value="submit" onclick="onsubmit()"/>
</form>
<script>
 $(function()
 $("#autoNum").autoNumeric();
 });
  function onsubmit(){
    $("#autoNum").val($("#autoNum").autoNumeric("get"));
    document.f1.submit();
  }
</script>

We can get the value with out the currency symbol using the autoNumeric public method get.

Madhu
  • 2,416
  • 3
  • 15
  • 33
1

Try this code on your server side, this works.

string x = "€650,-";
x = Regex.Replace(x, "[^0-9,]", "");
x = x.Split(',')[0];

Output:

650

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
1

Now this is possible with plugin default options.

new AutoNumeric(".money-val", { unformatOnSubmit: true  });

After form submission, it automatically formatted again on mouse hover on input field.

unformatOnHover:true
Rajitha Bandara
  • 743
  • 1
  • 10
  • 16
  • This is now the way to go with the latest version of AutoNumeric. However, if you want to submit the unformatted data, and stay on the same page with the formatted data in the form, then you'd prefer to use the [specialized form functions](https://github.com/autoNumeric/autoNumeric/#form-functions) like `anElement.formSubmitNumericString(callback);`. If you want to reformat all the unformatted values, just use `anElement.formReformat();`. – Alex Aug 21 '18 at 18:07