0

I'm adding dynamic input fields to a web form. I'm validating these using this Jquery Mask plugin.

It works fine on the initial input, but not the ones added dynamically.

My input HTML is;

<input type="text" id="mac_address" name="MacAddress[]">

My button HTML is;

<button class="add-mac" type="button" title="Add">Add</button>

The syntax used to validate the input is (works on first input only);

$('#mac_address').mask('ZZ-ZZ-ZZ-ZZ-ZZ-ZZ', {
    translation: {
        'Z': {
            pattern: /[A-Fa-f0-9]/,
            optional: false,
        }
    }
});

How can I add this validation to the dynamic inputs?

I have tried adding the code to a each loop although it doesn't work.

$('.add-mac').click(function() {
    $('#mac_address').each(function() {
        $(this).mask('ZZ-ZZ-ZZ-ZZ-ZZ-ZZ', {
            translation: {
                'Z': {
                    pattern: /[A-Fa-f0-9]/,
                    optional: false,
                }
            }
        });
    });
});

Any advice is appreciated.

2
  • You can't/shouldn't use same id to multiple elements in a single page Commented Aug 3, 2018 at 12:12
  • $('#mac_address').each(function() { does not make any sense. There can only be one DOM element with the ID "mac_address" on the page. Commented Aug 3, 2018 at 12:13

2 Answers 2

1

Added class to input

  <input type="text" class="mac_address" id="mac_address" name="MacAddress[]">
    <button class="add-mac" type="button" title="Add">Add</button>

    <script>
    var count = 1;
    $('.add-mac').click(function() {

     $('.mac_address').last().clone().attr('id', 'mac_address' + count).val('').insertBefore($(this));
    count ++; 
    });
    $(document).on('keyup', '.mac_address', function(){
      $('.mac_address').mask('ZZ-ZZ-ZZ-ZZ-ZZ-ZZ', {
        translation: {
            'Z': {
                pattern: /[A-Fa-f0-9]/,
                optional: false,
            }
        }
    });
    })
      $('.mac_address').mask('ZZ-ZZ-ZZ-ZZ-ZZ-ZZ', {
        translation: {
            'Z': {
                pattern: /[A-Fa-f0-9]/,
                optional: false,
            }
        }
    }); 
</script>

https://codepen.io/vommbat/pen/MBXeoj

Sign up to request clarification or add additional context in comments.

Comments

0

change the id to class

<input type="text" class="mac_address" name="MacAddress[]">
<button class="add-mac" type="button" title="Add">Add</button>

<script>
$('.add-mac').click(function() {
    $('.mac_address').each(function() {
        $(this).mask('ZZ-ZZ-ZZ-ZZ-ZZ-ZZ', {
            translation: {
                'Z': {
                    pattern: /[A-Fa-f0-9]/,
                    optional: false,
                }
            }
        });
    });
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.