Friday, April 22, 2016

Multiple Email address Validations using Regular Expression in JQuery

Multiple Email address Validations using Regular Expression in NewForm.aspx and before saving the item.

1) "To", CC, BCC  address are Multiline of text field with Plain text type aloow

===================================================================
function PreSaveAction()
{

var emailReg = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;


var To_emailaddressVal = $("textarea[title$='To Required Field']").text();
var Cc_emailaddressVal = $("textarea[title$='Cc']").text();
var Bcc_emailaddressVal = $("textarea[title$='Bcc']").text();

var To_emails=To_emailaddressVal.split(';');
var Cc_emails=Cc_emailaddressVal.split(';');
var Bcc_emails=Bcc_emailaddressVal.split(';');


//For loop for each email address in To
for (i = 0; i < To_emails.length; i++)
{
 if(To_emails[i].trim()!="")
 {

   if(!emailReg.test(To_emails[i].trim()))
   {
      alert("Please enter valid email address in To");
      return false;
   }
  }
}

//For loop for each email address in CC
for (i = 0; i < Cc_emails.length; i++)
{
 if(Cc_emails[i].trim()!="")
 {

   if(!emailReg.test(Cc_emails[i].trim()))
   {
      alert("Please enter valid email address in Cc");
      return false;
   }
  }
}

//For loop for each email address in BCC
for (i = 0; i < Bcc_emails.length; i++)
{
 if(Bcc_emails[i].trim()!="")
 {

   if(!emailReg.test(Bcc_emails[i].trim()))
   {
      alert("Please enter valid email address in Bcc");
      return false;
   }
  }
}


return true;
}