Email address validation not working :(

Hi,

I am making a script to validate email addresses.

I first used a regular expression.

Then, I checked if two or more symbols are next to each other using loops. However, my code doesn’t work. :frowning:

Please help me out.

Thanks in advance!!!

<html>
<head>
<title>Validating email addresses using JavaScript</title>
<script type="text/javascript">
function validate()
{
var email = new RegExp("^[A-Z0-9a-z]{1}[A-Z0-9a-z|\\._]*[A-Z0-9a-z]{1}[@]{1}[A-Z0-9a-z]{1}[A-Z0-9a-z-\\.]*[A-Z0-9a-z]{1}[\\.]{1}[A-Za-z]{3,4}$","i");
var given = document.getElementById("email").value;
document.getElementById("email").value.toLowerCase();
var result = email.test(given);
	if (result==false)
	{
	return false;
	}
	else
	{
	var num =0;
		while (num<(given.length)-1 && num!=-1)
		{
		num = given.indexOf(".",num)
			if (num>0 && (num<(given.length)-1))
			{
				if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@" || given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
				{
				return false;
				}
			}
			else if (num==0)
			{
				if (given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
				{
				return false;
				}
			}
			else if (num==(given.length)-1)
			{
				if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@")
				{
				return false;
				}
			}
			if (num<(given.length)-1 && num!=-1)
			{
			num=num+1;
			}
		}
		if (given.indexof("-",0)!=-1)
		{
		num =0;
			while (num<(given.length)-1 && num!=-1)
			{
			num = given.indexOf("_",num)
				if (num>0 && (num<(given.length)-1))
				{
					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@" || given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
					{
					return false;
					}
				}
				else if (num==0)
				{
					if (given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
					{
					return false;
					}
				}
				else if (num==(given.length)-1)
				{
					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@")
					{
					return false;
					}
				}
				if (num<(given.length)-1 && num!=-1)
				{
				num=num+1;
				}
			}	
		}
		if (given.indexOf("_",0)!=-1)
		{
		num =0;
			while (num<(given.length)-1 && num!=-1)
			{
			num = given.indexOf("-",num)
				if (num>0 && (num<(given.length)-1))
				{
					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@" || given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
					{
					return false;
					}
				}
				else if (num==0)
				{
					if (given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
					{
					return false;
					}
				}
				else if (num==(given.length)-1)
				{
					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@")
					{
					return false;
					}
				}
				if (num<(given.length)-1 && num!=-1)
				{
				num=num+1;
				}
			}	
		}
	}	
}
</script>
</head>
<body>
<form method="post" action="doesnotexists.php" onsubmit="return validate()">
<input type="text" name="email"><br />
<input type="submit" value="Check">
</form>
</body>
</html>
```html4strict

function validateEmail(email) 
{ 
 var re = /^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\
".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA
-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/ 
 return email.match(re) 
}

Thanx 4 ur reply!

However, it didn’t work out. :frowning:

Please see if you can help me out.

Actually I’ve written a 1 line snippet that works in PHP, so I think that JavaScript doesn’t support PCRE(Perl Compatible Regular Expressions), as PHP does. :frowning:

how about this?

function validateEmail(email) {
    var mail_filter  = /^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$/;
    if( mail_filter.test(email) ){
        // valid
    } else {
        // invalid
    }
}

Thanx again!

But this also accepts ‘sanchit@-algo.-csail.-mit.edu’ & ‘sanchit@algo.-csail.-mit.edu’,etc.

I found some errors in my file - I had written ‘name’ instead of ‘id’ & had missed 3 ';'s.

Now, I have fixed it.

However, you taught me a very great thing - I had been wrongly taught that JavaScript regular expressions are not PCRE. I saw grouping of units in your code & that prompted me to develop my own regular expression using grouping -

^[a-z0-9A-Z]{1,}[\\._]{0,1}[a-z0-9A-Z]{0,}){1,}[a-z0-9A-Z]{1}[@]([a-z0-9A-Z]{1,}[\\._\\-]{0,1}[a-z0-9A-Z]{0,}){1,}[a-z0-9A-Z]{1}[\\.]{1}[a-zA-Z]{2,4}$

It works for most real life examples(that I tested now; please tell me in case I am wrong). So I’ll use it instead of those long loops.

Thanks again! :slight_smile:

Yeah, I think you are onto something there!
Let me know if you have any real-world issues with it going forward!

Thanks! :slight_smile: