[英]jquery email extractor live from textarea
Okay so i have 2x textarea with different id's. 好的,所以我有2x个具有不同ID的textarea。 I'm trying to extract the emails pasted into the first textarea and then displayed into the second textarea.
我正在尝试提取粘贴到第一个文本区域中的电子邮件,然后将其显示到第二个文本区域中。 I know my jquery work for extracting and sorting emails from text in a page, but i cant seem to get it to work with textarea's.
我知道我的jquery可以从页面中的文本中提取和排序电子邮件,但是我似乎无法使其与textarea一起使用。
My code for the textarea's is as followed: 我的textarea代码如下:
<textarea id="email-extractor" class="email-extractor-textarea"></textarea>
<textarea id="email-extracted" class="email-extractor-textarea"></textarea>
My code for the is as followed: 我的代码如下:
<script>
function extractEmails (text)
{
return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
function eliminateDuplicates (arr) {
var i;
var len=arr.length;
var out=[];
var obj={};
for (i=0;i<len;i++) {
obj[arr[i]]=0;
}
for (i in obj) {
out.push(i);
}
return out;
}
var emailsFullList = [];
$('#email-extractor').keyup(function( index ) {
var emails = extractEmails($(this).text());
if (!emails) {
$('#email-extracted').text('** No emails in comment');
} else if (emails.length < 3) {
emailsFullList = emailsFullList.concat(emails);
} else {
$('#email-extracted').text('** Ignoring comment, too many email addresses ('+emails.length+').');
}
});
emailsFullList = eliminateDuplicates(emailsFullList);
$('#email-extracted').text(emailsFullList.join());
</script>
Your code seems to be working the only thing is that you were not adding the text to the second text area correctly: 您的代码似乎在起作用,唯一的事情是您没有将文本正确添加到第二个文本区域:
DEMO : http://jsfiddle.net/nkLvwtLa/ 演示 : http : //jsfiddle.net/nkLvwtLa/
$('#email-extractor').keyup(function (index) {
var emails = extractEmails($(this).val());
console.log(emails);
if (!emails) {
$('#email-extracted').text('** No emails in comment');
} else if (emails.length < 3) {
var text = '';
for (var i = 0; i < emails.length; i++) {
text += emails[i] + ',';
}
$('#email-extracted').text(text);
} else {
$('#email-extracted').text('** Ignoring comment, too many email addresses (' + emails.length + ').');
}
});
Try this... 尝试这个...
function extractEmails (text) { return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)/gi); } function eliminateDuplicates (arr) { var i; var len=arr.length; var out=[]; var obj={}; for (i=0;i<len;i++) { obj[arr[i]]=0; } for (i in obj) { out.push(i); } return out; } var emailsFullList = []; $('#email-extractor').keyup(function( index ) { var emails = extractEmails($(this).val()); if (!emails) { $('#email-extracted').val('** No emails in comment'); } else if (emails.length < 3) { emailsFullList = emailsFullList.concat(emails); } else { $('#email-extracted').val('** Ignoring comment, too many email addresses ('+emails.length+').'); } emailsFullList = eliminateDuplicates(emailsFullList); $('#email-extracted').val(emailsFullList.join()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <textarea id="email-extractor" class="email-extractor-textarea"></textarea> <textarea id="email-extracted" class="email-extractor-textarea"></textarea>
You need to move these lines... 您需要移动这些行...
emailsFullList = eliminateDuplicates(emailsFullList);
$('#email-extracted').text(emailsFullList.join());
into your .keyup()
otherwise they are only run on page load. 到您的
.keyup()
否则它们仅在页面加载时运行。 Also, when working with textarea
I believe you should be using .val()
rather than .text()
to get/set the value 另外,在使用
textarea
时,我相信您应该使用.val()
而不是.text()
来获取/设置值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.