
/************ Write this code onBlur of any textbox OR as per your requirement **********************************************************

*	You can write following code on onBlur of any textbox OR onSubmit of any Submit button OR as per your requirement.

*	"textName" is the name of text box where you want to check the email.
*
*	onKeyDown = "javascript: if(event.keyCode==13) event.keyCode=9;"
*	onBlur or onClick = "javascript: checkEmailAddress(this.form.textName);"

****************************************************************************************************************************************/

/***************************** Trim function for removing the white space from starting & end position. ********************************/

function trim_Email(str)
{
	var strReturn = str;
	var i = 0;
	
	for(i=0; i<strReturn.length; i++)
	{
		if(!(strReturn.charAt(i) == ' '))
		break;
	}

	strReturn = strReturn.substring(i);

	for(i=strReturn.length; i>0; i--)
	{
		if(!(strReturn.charAt(i-1) == ' '))
		break;
	}

	strReturn = strReturn.substring(0,i);
	return strReturn;
}
/***************************************************************************************************************************************/

/************************** Function return false if email-id have any special character ***********************************************/

function CheckSpecialChar_Email(getString)
{
	var specialChar_Email = new Array("~","!","#","$","%","^","`","&","*","(",")","=","+","|","\\","{","}","[","]",";",":","\"","\'","<",">",",","?","/"," ");

	var Length_specialChar = specialChar_Email.length;
	var Flag = "false";

	for(var i=0; i<Length_specialChar; i++)
	{
		var val = specialChar_Email[i];

		for(var j=0; j < getString.length ; j++)
		{
			if(getString.charAt(j)== val)
			{
				Flag = "true";
				break;
			}
		}
	}

	return Flag;
}
/***************************************************************************************************************************************/

/************************************* Check Email-Id is in correct format. ************************************************************/

function checkEmailAddress(emailId)
{
	var EmailAddress1 = trim_Email(emailId.value);
	var EmailAddress = EmailAddress1.toLowerCase();
	emailId.value = EmailAddress;
	var EmailAddressLength = EmailAddress.length;//getting the length
	var at = EmailAddress.indexOf('@',0)  	//getting the position of '@'
	var dot = EmailAddress.indexOf('.',0) 	//getting the position of '.'
	
	var count = 0;
	var returnFlag = "false";
	var ckmail = "true";
	var flag = "t";
	var n = 0;

	if(EmailAddress != "")
	{
		returnFlag = CheckSpecialChar_Email(EmailAddress);
	}
//	For Special Character.
		if(returnFlag == "true")
		{
			alert("Invalid E-mail Address!");
			emailId.select();
			emailId.focus();
			return false;
		}
//	First & last character should not be Hyphen or underscore.
		if(EmailAddress.charAt(0) == '_' || EmailAddress.charAt(EmailAddressLength-1) == '_' || EmailAddress.charAt(0) == '-' || EmailAddress.charAt(EmailAddressLength-1) == '-')
		{ 
			alert("Invalid E-mail Address!");
			emailId.select();
			emailId.focus();
			return false;
		}
//	First character should not be number.
		//var beginnumber = EmailAddress.charAt(0);
		//if(!isNaN(beginnumber)) 
		//{ 
			//alert("Invalid E-mail Address!");
			//emailId.select();
			//emailId.focus();
			//return false;
		//}		
//	Checking whether the first & last char is a '.' and '@'
		if((EmailAddress.charAt(0) == ".") || (EmailAddress.charAt(EmailAddressLength-1) == ".") || (EmailAddress.charAt(0) == "@") || (EmailAddress.charAt(EmailAddressLength-1) == "@") )
		{ 
			alert("Invalid E-mail Address!");
			emailId.select();
			emailId.focus();
			return false;
		}
//	Checking whether '@' and '.' are present 
		if((EmailAddress.indexOf('@' ,0) == -1)||(EmailAddress.indexOf('.',0) == -1))
		{
			alert("Invalid E-mail Address!");
			emailId.select();
			emailId.focus();
			return false;
		}

for(var i=0;i<EmailAddressLength;i++)
	{
//	Checking whether two corresponding @ are there 
			if((EmailAddress.charAt(i) == "@") && (EmailAddress.charAt(i+1) == "@"))
			{
				alert("Invalid E-mail Address!");
				emailId.select();
				emailId.focus();
				return false;
			}

			if(EmailAddress.charAt(i) == "@")
			{
				count = count+1;
			}
		}

		if(count > 1)
		{
			alert("Invalid E-mail Address!");
			emailId.select();
			emailId.focus();
			return false;
		}
//	Checking out whether '.@','@.'
			for(i=0;i<EmailAddressLength;i++)
			{
			if((EmailAddress.charAt(i) == ".") && (EmailAddress.charAt(i+1) == "@") || ((EmailAddress.charAt(i) == "@") && (EmailAddress.charAt(i+1) == ".")))
				{
				alert("Invalid Email Address!");
				emailId.select();
				emailId.focus();
				status = "false";
				return false;
				}
			
//	Checking out whether '@-','@_'
		if(((EmailAddress.charAt(i) == "@") && (EmailAddress.charAt(i+1) == "-")) || ((EmailAddress.charAt(i) == "@") && (EmailAddress.charAt(i+1) == "_")))
				{
				alert("Invalid Email Address!");
				emailId.select();
				emailId.focus();
				return false;
				}
			
// Checking two consecutive '.'
		if((EmailAddress.charAt(i) == ".") && (EmailAddress.charAt(i+1) == "."))
		{
		alert("Invalid Email Address!");
		emailId.select();
		emailId.focus();
		return false;
		}
	}

// Checking out underscore after '@'
var strat =  EmailAddress.substring(at+1);//getting the string after '@'
var stratLength =strat.length;
for(i=0;i<stratLength;i++)
	{
		if(strat.charAt(i) == "_")
		{
		alert("Invalid Email Address!");
		emailId.select();
		emailId.focus();
		return false;
		}
	}

//	Checking out whether '-.','.-' after '@'
for(i=0;i<stratLength;i++)
	{
	if(((strat.charAt(i) == "-") && (strat.charAt(i+1) == ".")) || ((strat.charAt(i) == ".") && (strat.charAt(i+1) == "-")))
	{
	alert("Invalid Email Address!");
	emailId.select();
	emailId.focus();
	return false;
	}
}
var dotat = strat.indexOf('.',0)  //getting the postition of '.' after '@'
var strdot =  strat.substring(dotat+1); //getting the string between '@' and '.'
// Checking the length of the characters of Domain and sub-domain it should not be less than 2 characters
if(dotat < 2)
	{
	alert("Invalid Email Address!");
	emailId.select();
	emailId.focus();
	return false;

	}
for(i=dotat;i<stratLength;i++)
	{
	 if((strat.charAt(i) == ".") && (strat.charAt(i+2) == "."))
		{
		alert("Invalid Email Address!");
		emailId.select();
		emailId.focus();
		return false;
		}

	}
if(strat.charAt(stratLength-2) == ".")
	{
	alert("Invalid Email Address!");
	emailId.select();
	emailId.focus();
	return false;
	}



return true;
}//end of function
/***************************************************************************************************************************************/
