?
Solved

javascript email validation

Posted on 2007-11-17
14
Medium Priority
?
1,566 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

777 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