[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1574
  • Last Modified:

javascript email validation

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
cempasha
Asked:
cempasha
  • 5
  • 5
  • 4
1 Solution
 
nizsmoDeveloperCommented:
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
 
Göran AnderssonCommented:
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
 
cempashaAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nizsmoDeveloperCommented:
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
 
cempashaAuthor Commented:
it brings out the error saying enter a valid email address
0
 
nizsmoDeveloperCommented:
when you enter a valid email address? or invalid? what email did you try?
0
 
cempashaAuthor Commented:
yes when it is checking the email. I've used my regular email as cempasha@mydomain.com
0
 
Göran AnderssonCommented:
Try this:

var splitted = email.match(/^[\w\-%~\.]+@[\w\-\.]+\.[\w]{2,4}$/);
0
 
cempashaAuthor Commented:
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
 
nizsmoDeveloperCommented:
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
 
Göran AnderssonCommented:
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
 
cempashaAuthor Commented:
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
 
nizsmoDeveloperCommented:
glad to help :)
0
 
Göran AnderssonCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now