Solved

javascript email validation

Posted on 2007-11-17
14
1,555 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
  • 5
  • 5
  • 4
14 Comments
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility
it brings out the error saying enter a valid email address
0
 
LVL 21

Expert Comment

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

Author Comment

by:cempasha
Comment Utility
yes when it is checking the email. I've used my regular email as cempasha@mydomain.com
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
Try this:

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

Author Comment

by:cempasha
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
glad to help :)
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now