javascript - Validate tags using tagit - Stack Overflow

I'm using the following tag-it plugin: .I would like to validate the tags, the input would be ema

I'm using the following tag-it plugin: /.

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

My code:

HTML

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

JavaScript (jQuery)

$(document).ready(function () {
        $('#editOnClick > ul').tagit({
            select:true,
            triggerKeys:['ma', '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.

I'm using the following tag-it plugin: http://widen.github.io/jQuery-Tagit/.

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

My code:

HTML

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

JavaScript (jQuery)

$(document).ready(function () {
        $('#editOnClick > ul').tagit({
            select:true,
            triggerKeys:['ma', '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.

Share Improve this question edited May 14, 2014 at 10:21 Chris 6,0722 gold badges35 silver badges57 bronze badges asked May 14, 2014 at 8:53 user2862120user2862120 831 silver badge8 bronze badges 6
  • possible duplicate of Validate email address in JavaScript? – ohaal Commented May 14, 2014 at 8:59
  • 1 The tags are not really relevant for this question. You are asking how to validate e-mails in JavaScript, and that has already been asked. – ohaal Commented May 14, 2014 at 9:00
  • Hi, thanks for your response. I would like to know how to validate the tags. – user2862120 Commented May 14, 2014 at 9:07
  • 1 Maybe you should remove the email part from the question. But in that case an obvious question pops up: have you read the documentation? Have you tried the callbacks provided there? If they did not work, or you have problems with them, please add details. – kapa Commented May 14, 2014 at 9:26
  • 1 @ Kapa Thanks for your help. I'm very new to jquery, I have checked the documentation and tried a few things but I can't seem to figure out what to do. I updated my code to things I have tried. – user2862120 Commented May 14, 2014 at 9:55
 |  Show 1 more ment

1 Answer 1

Reset to default 7

I'd use the tagsChanged callback to detect when a new tag has been added, and then validate, and remove it if not valid. I see you've used beforeTagAdded - I'm not sure where you've got that from? But I don't know the plugin.

The below code does the job. Updated JSFiddle

    $(document).ready(function () {
        $('#editOnClick > ul').tagit({
            select:true,
            triggerKeys:['ma', '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);
        }

    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745540364a4632099.html

相关推荐

  • javascript - Validate tags using tagit - Stack Overflow

    I'm using the following tag-it plugin: .I would like to validate the tags, the input would be ema

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信