Checking tags with tagit

I am using the following tag-it plugin: http://widen.imtqy.com/jQuery-Tagit/ .

I would like to check the tags, the input will be email addresses. How can i do this?

My code is:

HTML

<div id="editOnClick" class="example">
    <ul name="email[]" id="email"></ul>
</div>

JavaScript (jQuery)

$(document).ready(function () {
        $('#editOnClick > ul').tagit({
            select:true,
            triggerKeys:['comma', 'enter', 'space', 'semicolon', 'tab'],
            tagSource:"view_email_get_emails.php",
            editOnClick:true 
            beforeTagAdded: function(event, ui) {
              return isEmail($('#email').tagit("tags"))}  

 });

function isEmail(email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
}
        });
        });

I added my code to JSFiddle .

+4
source share
1 answer

I would use a callback tagsChangedto determine when a new tag was added, and then check and remove it if it is not valid. I see you used it beforeTagAdded- I'm not sure where you got it from? But I do not know the plugin.

. JSFiddle

    $(document).ready(function () {
        $('#editOnClick > ul').tagit({
            select:true,
            triggerKeys:['comma', 'enter', 'space', 'semicolon', 'tab'],
            tagSource:"view_email_get_emails.php",
            editOnClick:true,
            tagsChanged: function(tagValue, action, element){
                if (action == 'added'){
                    if (!isEmail(tagValue)){
                        $('#editOnClick > ul').tagit("remove", 'tag', tagValue);

                    }
                }

            });


        function isEmail(email) {
            var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            return regex.test(email);
        }

    });
+7

Source: https://habr.com/ru/post/1540489/


All Articles