Count E-mails in textarea

tekagami 1 Tallied Votes 371 Views Share

1) onkeyup send textarea values to script
2) converts textarea value to array
3) loops thru the array
4) uses regex to check if its an email address
5) changes inner html of recipient div

LastMitch commented: Thanks for the sharing! +12
<script>
function countaddresses(obj){
lineas = 0;
var string = obj.value;
var array = string.split('\n');
var regMail = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/;

 for (i = 0; i < array.length; i++) {
 

		if(regMail.test(array[i]) == true) {

			lineas++;

		}
}

document.getElementById('lemails').innerHTML=lineas;
}
</script>
<textarea name="emails" class="widel" onkeyup="countaddresses(this)"></textarea>
			<div id="lemails"></div>
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Just a tip: your regex does not work on TLD's larger than three characters (.info, .museum)

tekagami 23 Newbie Poster

Just change var regMail = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/; to

var regMail = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,7})$/;

Member Avatar for LastMitch
LastMitch

Count E-mails in textarea

This is a nice simple codesnippet. it does it job to count emails in the textares. Thanks for sharing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.