检测何时将文本输入到textarea中并相应地更改它 [英] Detect when text is entered into the textarea and change it correspondingly

查看:172
本文介绍了检测何时将文本输入到textarea中并相应地更改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 textarea 用户可以输入或粘贴其他人的电子邮件地址,并在按下提交按钮后发送邀请。在提交表单之前,每封电子邮件必须用逗号分隔并且有效 - 验证由 jQuery验证插件处理。 &安培; 多邮件方法



<问题



有些人直接从他们的电子邮件客户端粘贴电子邮件地址,而这些电子邮件通常都是奇怪的格式 - 包含姓名和姓氏实际的电子邮件或电子邮件被包裹在< >。例如:
The Dude< the.dude@gmail.com>,Dudette< thedudette193@gmail.com>



问题



我想要做的是使用jquery从批量文本中提取所有电子邮件地址,但是我遇到了问题,一块代码与我的 textarea 工作 - 我不知道从哪里开始。



我该如何使用在输入逗号后或当焦点从 textarea 移开时,从上述答案的代码中提取输入到 textarea 中的每封电子邮件C $ C>?因此,如果我粘贴The Dude< the.dude@gmail.com> 并在其后输入,或切换焦点,输入的值将变为 the.dude@gmail.com

解决方案我猜这样的事情:

  var textarea = $('#emails'); 
$ b textarea.on({
keyup:function(e){
if(e.which === 188)check();
},
blur:检查
});

函数check(){
var val = $ .trim(textarea.val()),
err ='';

if(!val.length){
err ='没有输入?';
return;
}

var emails = val.split(','),
notvalid = [],
temp = [];

$ .each(emails,function(_,mail){
mail = $ .trim(mail);
if(mail.length){
var m = mail.match(/([a-zA-Z0-9 ._-] + @ [a-zA-Z0-9 ._-] + \。[a-zA-Z0-9 ._-] (m){
temp.push(m);
}其他{
temp.push(邮件);
无效。推送(邮件)
}
}其他{
temp.push(邮件);
}
if(notvalid.length)err ='无效的电子邮件:' + notvalid.join(',');
});

$('#error')。html(err);
textarea.val((temp.length?temp:emails).join(','));
}

FIDDLE


I have a textarea where users can enter or paste email addresses of other people and send them an invite after pressing Submit button. Each email must be seperated with a comma and valid before the form is submitted - validation is taken care of by jQuery Validate plugin & multiemail method.

Problem

Some people paste email addresses directly from their email clients and those emails are often in a weird format - containing name and surname before the actual email, or the email is wrapped in < >. For example: "The Dude" <the.dude@gmail.com>, "The Dudette" <thedudette193@gmail.com>

Question

What I want to do is to Extract all email addresses from bulk text using jquery, but I'm having problems integrating this piece of code to work with my textarea - I don't know where to start.

How could I use the code from the above answer to extract each email entered into the textarea after a comma is typed or when the focus is moved away from textarea? So if I paste "The Dude" <the.dude@gmail.com> and type , after it or switch focus away, the entered value would change to the.dude@gmail.com.

解决方案

I'm guessing something like this :

var textarea = $('#emails');

textarea.on({
    keyup: function(e) {
        if (e.which === 188) check();
    },
    blur: check    
});

function check() {
    var val  = $.trim(textarea.val()),
        err  = '';

    if (!val.length) {
        err = 'No input ?';
        return;
    }

    var emails   = val.split(','),
        notvalid = [],
        temp     = [];

    $.each(emails, function(_,mail) {
        mail = $.trim(mail);
        if ( mail.length ) {
            var m = mail.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
            if (m) {
                temp.push(m);
            }else{
                temp.push(mail);
                notvalid.push(mail)
            }
        }else{
            temp.push(mail);
        }
        if (notvalid.length) err = 'Not valid emails : ' + notvalid.join(', ');
    });

    $('#error').html(err);
    textarea.val((temp.length ? temp : emails).join(', '));
}

FIDDLE

这篇关于检测何时将文本输入到textarea中并相应地更改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆