Reputation: 1
I want to validate a textarea for email and also I want that same textarea to allow a number of emails. I need this for a refer a friend option.
HTML part:
<textarea class="span12" id="demo"></textarea>
Upvotes: 0
Views: 6118
Reputation: 11
Here's one way to validate email addresses separated by comma (,).
There's a submit and reset button, a textarea for taking the mails, and function validateform
to validate the emails in the textarea.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><html>
<style>
</style>
</head>
<body>
<div class="input">
<p class="label"><h2>*Email Address:</h2>
<textarea placeholder="Enter ur mails by separator (,) " cols="30" rows="10" id="email_val" name="mail"></textarea>
</p>
<p id="demo"></p>
<p class="label"><input type="submit" onclick="ValidateForm()"></p>
<p class="label"><input type="reset" onclick="history.go(0)"></p>
</div>
<script>
function ValidateForm() {
var emailList = $("#email_val").val().split(',');
for (i = 0; i < emailList.length; i++)
{
for (i = 0; i < emailList.length; i++) {
// var expr =/^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,6})$/;
var expr = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var result = expr.test(emailList[i]);
if (!result) {
document.getElementById("demo").style.color="red";
document.getElementById("demo").innerHTML = "sorry enter a valid email adrress";
return false;
}
}
document.getElementById("demo").style.color="blue";
if(emailList.length==1)
{
document.getElementById("demo").innerHTML = "Ur email address is successfully submitted";
return true;
} else{
document.getElementById("demo").innerHTML = "Ur "+emailList.length+" email addresses are successfully submitted";
}
}
}
</script>
</body>
</html>
*
Upvotes: 1
Reputation: 535
Your question is really two questions: 1. How do I validate an email from an input field? 2. How do I validate multiple emails from an input field?
For #1, I'd suggest using the tried and true jQuery Validation library instead of rolling your own email validator. Docs here: http://jqueryvalidation.org/email-method
After including the jQuery Validation library, you'd call it like this:
$( "#demo" ).validate({
rules: {
field: {
required: false,
email: true
}
}
});
Of course that'll only work for a single email address.
To answer #2, you'll have to split the value of the textarea by some delimiter (e.g. whitespace, commas, etc) and then validate each string separately.
Upvotes: 1
Reputation: 3106
try this
function ValidateEmails() {
var emailList= $("#demo").val().split(',');
for (i=0;i<emailList.length;i++)
{
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(emailList[i]);
}
}
you will need to enter coma seperated emailids in your text area
Upvotes: 2
Reputation: 93
function validateEmail(field) {
var regex=/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
return (regex.test(field)) ? true : false;
}
function validateMultipleEmailsCommaSeparated(value) {
var result = value.split(",");
for(var i = 0;i < result.length;i++)
if(!validateEmail(result[i]))
return false;
return true;
}
This will help you... with some of example... [email protected],[email protected]
Upvotes: 1
Reputation: 3559
Try this:
$.validator.addMethod('demo', function(value, element) {
if (this.optional(element)) return true;
var flag = true;
var addresses = value.replace(/\s/g,'').split(',');
for (i = 0; i < addresses.length; i++) {
flag = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(addresses[i]);
}
return flag;
}, '');
Upvotes: 0
Reputation: 4899
There is too much different code for email validation - just look around:
Also, it is a bad idea to make textarea with emails. You should watch for separator, spaces, etc... Better to make a simple input, add emails to script array and then display as removable buttons.
Upvotes: 0