jQuery submit function add data several times

50
December 03, 2019, at 00:30 AM

I have this function I use to store items (voucher codes) based on some rules...

$("#serial_number").submit(function (e) {
        e.preventDefault();
console.log('started');
        if (param == 'groupon' || param == 'livingsocial') {
            var secretS = $('#serial').val();
            var secretP = $('#pin').val();
            if ((/(?=[a-fA-F0-9]*[a-fA-F])[a-fA-F0-9]{10}/.test(secretS) === true) || (secretS.length == 13)) {

                var s = $('#serial').val();
                var p = $('#pin').val();
                var item = {}
                item["serial"] = s;
                item["pin"] = p;
                item["product_code"] = '74';
                item["promo_code"] = '74';
                item["covers"] = 2;
                jsonObj.push(item);

as you can see based on rexEg rule I write in IF condition I add an item to jsonObj so if it's valid I push into jsonObj an item.

If I type just once the correct serial based on rule like 123456789a everything is fine and I have this [{serial: "123456789a", pin: undefined}] which is fine...

Problem is if:

  1. attempt - User type something like 1234 and try to submit a form but based o rules its item not stored into jsonObj

  2. attempt - User type ggghhh55 also not stored into jsonObj

  3. user finally submit correct serial like (123456789a) but then its added 3 times so I have this:

    [{serial: "123456789a", pin: undefined}, {serial: "123456789a", pin: undefined}, {serial: "123456789a", pin: undefined}]

Why? Why submit to form add item 3 times... should add it just once ...

Finally when the user enter correct serial then the form is submitted number of previous attempts ... so if there is 3 unsuccessful attempts form is submitted 3 times ...

Answer 1

Seems like you need to reset your jsonObj after each attempt

READ ALSO
How to use a select's value to show a certain div(a div of radio buttons)?

How to use a select's value to show a certain div(a div of radio buttons)?

My goal: The user picks selection 1 and the corresponding div will showThe user picks selection 2, the selection 1 div disappears and shows the selection 2's div

74
How can I run jQuery function only if user hasn’t scrolled?

How can I run jQuery function only if user hasn’t scrolled?

I’ve been trying to incorporate an auto scroll function on my website (live here)I want it to scroll to the content 6 seconds after load, but only if the user is on the very top of the page and hasn’t scrolled enough to uncover the #introduction element

65
how can get only numbers from the input

how can get only numbers from the input

I have on keyup and mask for phone numbers, I need to get only digits, not symbols on mask, but it is not working, please let me know my mistake

94
CropperJS getCroppedCanvas() returns null on second initialization

CropperJS getCroppedCanvas() returns null on second initialization

The method is called in the ready eventOn the first call it works

75