17

I'm using the jQuery plugin AutoNumeric but when I submit a form, I can't remove the formatting on the fields before POST.

I tried to use $('input').autonumeric('destroy') (and other methods) but it leaves the formatting on the text fields.

How can I POST the unformatted data to the server? How can I remove the formatting? Is there an attribute for it in the initial config, or somewhere else?

I don't want to send the serialized form data to the server (with AJAX). I want to submit the form with the unformatted data like a normal HTML action.

isherwood
  • 58,414
  • 16
  • 114
  • 157
4spir
  • 1,174
  • 2
  • 15
  • 31

11 Answers11

15

I wrote a better, somewhat more general hack for this in jQuery

$('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;
});

This code cleans form w/ error handling on not autoNumeric values.

4spir
  • 1,174
  • 2
  • 15
  • 31
  • nice, but for a sec or two (while form is submitting) I see the unformatted value. It's just a cosmetic issue, but it's little annoying. – Adrian Nov 06 '15 at 20:17
  • 9
    There is no need for this anymore, you can just use the following option now : `{ unformatOnSubmit : true }` – Alex Jan 11 '17 at 08:31
12

With newer versions you can use the option:

unformatOnSubmit: true
jpaoletti
  • 855
  • 1
  • 13
  • 21
2

Inside data callback you must call getString method like below:

        $("#form").autosave({
        callbacks: {
            data: function (options, $inputs, formData) {

                return $("#form").autoNumeric("getString");
            },
            trigger: {
                method: "interval",
                options: {
                    interval: 300000
                }
            },
            save: {
                method: "ajax",
                options: {
                    type: "POST",
                    url: '/Action',
                    success: function (data) {

                    }
                }
            }
        }
    });
ararog
  • 1,092
  • 10
  • 18
1

Use the get method.

'get' | returns un-formatted object via ".val()" or ".text()" | $(selector).autoNumeric('get');


<script type="text/javascript">
    function clean(form) {
        form["my_field"].value = "15";
    }
</script>

<form method="post" action="submit.php" onsubmit="clean(this)">
    <input type="text" name="my_field">
</form>

This will always submit "15". Now get creative :)


Mirrored raw value:

<form method="post" action="submit.php">
    <input type="text" name="my_field_formatted" id="my_field_formatted">
    <input type="hidden" name="my_field" id="my_field_raw">
</form>

<script type="text/javascript">
    $("#my_field_formatted").change(function () {
        $("#my_field").val($("#my_field_formatted").autoNumeric("get"));
    });
</script>

The in submit.php ignore the value for my_field_formatted and use my_field instead.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • get method returns the value of the input field. i need to submit the unformatted form. i don't want to get the unformatted value for a field. – 4spir Apr 04 '13 at 15:47
  • Then `get` the value and submit that? I'm assuming you have some custom `onsubmit` handler in place, if not, make one, you'll need it. – Halcyon Apr 04 '13 at 15:49
  • so it's not possible to submit the unformatted data without ajax request? – 4spir Apr 04 '13 at 15:50
  • Sure it is. You can modify the values of fields in your onsubmit handler. It wont be as nice and clean as AJAX but it's certainly doable. – Halcyon Apr 04 '13 at 15:55
  • if you can provide me this solution, i'll certainly accept your answer. :) – 4spir Apr 04 '13 at 15:56
  • i've already implemented something like that, but i thought there's a cleaner solution... are you sure there's no other way? – 4spir Apr 04 '13 at 16:03
  • AJAX could be a _clean_ solution. Another solution is that you mirror the value of the formatted field to a hidden field that contains just the value. The submit handler should ignore the formatted value and just look at the raw value, I'll update my answer. – Halcyon Apr 05 '13 at 12:23
  • i've answered the question above and i'll use this in my code – 4spir Apr 05 '13 at 12:36
1

You can always use php str_replace function

str_repalce(',','',$stringYouWantToFix);

it will remove all commas. you can cast the value to integer if necessary.

Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • why php? (fyi i used java on server side for the app.) wrong solution, not an answer. – 4spir Jun 11 '13 at 13:41
  • That's not matter. Java probably have methods with similar functionality. I see it as a solution because it will remove the formatting and that's want we want. why its important to do it client side? – Alireza Ahmadi Jul 21 '13 at 06:33
  • 1
    because it's much cleaner in a multi-layered application. read i.e. http://en.wikipedia.org/wiki/Loose_coupling for the idea. – 4spir Jul 22 '13 at 08:40
0
$("input.classname").autoNumeric('init',{your_options});
$('form').submit(function(){
   var form=$(this);
   $('form').find('input.classname').each(function(){
       var self=$(this);
       var v = self.autoNumeric('get');
       // self.autoNumeric('destroy');
       self.val(v);
   });
});

classname is your input class that will init as autoNumeric Sorry for bad English ^_^

0

There is another solution for integration which doesn't interfere with your client-side validation nor causes the flash of unformatted text before submission:

var input = $(selector);
var proxy = document.createElement('input');
proxy.type = 'text';
input.parent().prepend(proxy);
proxy = $(proxy);
proxy.autoNumeric('init', options);
proxy.autoNumeric('set', input.val())''
proxy.change(function () {
    input.val(proxy.autoNumeric('get'));
});
Jan Drábek
  • 146
  • 6
0

You could use the getArray method (http://www.decorplanit.com/plugin/#getArrayAnchor).

$.post("myScript.php", $('#mainFormData').autoNumeric('getArray'));

Alin A
  • 1
  • 2
0

I came up with this, seems like the cleanest way. I know it's a pretty old thread but it's the first Google match, so i'll leave it here for future

$('form').on('submit', function(){
    $('.curr').each(function(){
        $(this).autoNumeric('update', {aSign: '', aDec: '.', aSep: ''});;
    });
});
Piotrek
  • 61
  • 1
  • 7
0

Solution for AJAX Use Case

I believe this is better answer among all of those mentioned above, as the person who wrote the question is doing AJAX. So kindly upvote it, so that people find it easily. For non-ajax form submission, answer given by @jpaoletti is the right one.

// Get a reference to any one of the AutoNumeric element on the form to be submitted
var element = AutoNumeric.getAutoNumericElement('#modifyQuantity');

// Unformat ALL elements belonging to the form that includes above element
// Note: Do not perform following in AJAX beforeSend event, it will not work
element.formUnformat();

$.ajax({
    url: "<url>",
    data : {
        ids : ids,
        orderType : $('#modifyOrderType').val(),
        
        // Directly use val() for all AutoNumeric fields (they will be unformatted now)
        quantity : $('#modifyQuantity').val(),
        price : $('#modifyPrice').val(),
        triggerPrice : $('#modifyTriggerPrice').val()
    }
})
.always(function(  ) {
    // When AJAX is finished, re-apply formatting    
    element.formReformat();     
});
Pritesh Mhatre
  • 3,847
  • 2
  • 23
  • 27
0

autoNumeric("getArray") no longer works.

unformatOnSubmit: true does not seem to work when form is submitted with Ajax using serializeArray().

Instead use formArrayFormatted to get the equivalent serialised data of form.serializeArray()

Just get any AutoNumeric initialised element from the form and call the method. It will serialise the entire form including non-autonumeric inputs.

$.ajax({
    type: "POST",
    url: url,
    data: AutoNumeric.getAutoNumericElement("#anyElement").formArrayFormatted(),
)};