Solved

javascript email validation

Posted on 2007-11-17
14
1,563 Views
Last Modified: 2008-05-11
Hi all,
I'm using http://www.javascript-coder.com/html-form/javascript-form-validation.phtml this example for validating my forms. Everything seems to work fine however there is a little problem. For the email part it checks everything however it allows the data to be submitted in format of

example@.com (without domain name)
apart from this it checks all the details.
I've copy pasted the email code . Thanks in advance for your  help

cem
function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

Open in new window

0
Comment
Question by:cempasha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
14 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20305673
Try and use this regular expression pattern for the email:
/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20305681
Use this regular expression for the email:

/^[\w\-%~\.]+@[\w\-\.]+\.[\w]{2,4}$/

It's a bit stricter than the one you are using now. For example, it will not consider "@@@" to be a valid email address...
0
 
LVL 5

Author Comment

by:cempasha
ID: 20305694
thanks for both suggestions. can you put it in the code? I'm not good with javascript as I've just copy pasted the code as well.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:nizsmo
ID: 20305712
try replace this line:

var splitted = email.match("^(.+)@(.+)$");

with this line:

var splitted = email.match("^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
0
 
LVL 5

Author Comment

by:cempasha
ID: 20305718
it brings out the error saying enter a valid email address
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20305723
when you enter a valid email address? or invalid? what email did you try?
0
 
LVL 5

Author Comment

by:cempasha
ID: 20305729
yes when it is checking the email. I've used my regular email as cempasha@mydomain.com
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20305735
Try this:

var splitted = email.match(/^[\w\-%~\.]+@[\w\-\.]+\.[\w]{2,4}$/);
0
 
LVL 5

Author Comment

by:cempasha
ID: 20305743
it still says the same with this. shall I get rid of any statements at all ? I'm keeping the code same but just changing the part you have mentioned.
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 250 total points
ID: 20305770
Not sure why these are not working, however here is a function for validating email addresses. you can simply call it (with email address as argument) and it will return a TRUE for successful or FALSE for invalid email.

Then you can modify your function like this:


function validateEmailv2(email)
{
// a very simple email validation checking.
// you can add more complex email checking if it helps
    if(email.length <= 0)
        {
          return true;
        }
    var emailCheck = validateAddress(email); // call this function here!
    if(emailCheck)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
            var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}
function validateAddress(incoming) {
	var emailstring = incoming;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
		//old regex did not allow subdomains and dots in names
		//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
			alert('validated');
		  return true;
	} else {
			alert("Please check your email address!");
			return false;
	}
}

Open in new window

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20305810
I see. I overlooked how the regular expression match interacted with the rest of the code, so I didn't add any catches in the expression.

This works:

var splitted = email.match(/^([\w\-%~\.]+)@([\w\-\.]+\.[\w]{2,4})$/);
0
 
LVL 5

Author Comment

by:cempasha
ID: 20305945
i really dont understand why but still both functions doesnt seem to work. however I've used the

function validateAddress(incoming)
version and now it seems to be working fine. thanks a lot for your help
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20305948
glad to help :)
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20306143
I don't know what you are doing if you can't make it work. Here's a complete html page where I pasted the line In your function, and it works perfectly fine:

<html>
<head>
<script>

function validateEmailv2(email)
{
    if(email.length <= 0)
        {
          return true;
        }
    var splitted = email.match(/^([\w\-%~\.]+)@([\w\-\.]+\.[\w]{2,4})$/);
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
            var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
    return false;
}

</script>
</head>
<body onload="alert(validateEmailv2('cempasha@mydomain.com'));">

</body>
</html>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question