Solved

Validating Email with Regular Expression

Posted on 2013-11-12
5
272 Views
Last Modified: 2013-11-12
I have the following code checking emails on my contact form:

function regIsEmail(fData){ 
var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$"); 
return reg.test(fData); 
}

Open in new window


The problem is, email addresses with "." in them like "whats.up@gmail.com" get flagged as invalid.  Any ideas how I can allow for that "."?

Thank you for your help!
0
Comment
Question by:slightlyoff
[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
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 500 total points
ID: 39642434
var reg = new RegExp("^\w[0-9a-zA-Z.]+@[0-9a-zA-Z.]+\.[a-zA-Z]+$");

Open in new window

I've also taken the liberty to make some minor tweaks to your regex, such as checking for an alpha as the first char, and not allowing the last suffix to contain numbers. :-)
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39642498
Thanks for the quick response.  I substituted your var reg = new RegExp line with mine.  For some reason, it still isn't allowing the "whats.up@gmail.com" formatted address to go through.

here's what I have now:

function regIsEmail(fData){ 
				//var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
				var reg = new RegExp("^\w[0-9a-zA-Z.]+@[0-9a-zA-Z.]+\.[a-zA-Z]+$"); 
				return reg.test(fData); 
	}

Open in new window


Here's where it's called, just in case that has something to do with it:

		if (regIsEmail(document.getElementById("theEmail").value) != true){
				alert(document.getElementById("theEmail").value + " is not a valid email.");
				$("#theEmail").addClass('highs');
				$("#theEmail").focus();
				return false;
		} else $("#theEmail").removeClass('highs');

Open in new window


Thanks again for you quick reply!
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39642606
I ran that regex thru regexpal.com and whats.up@gmail.com was found by the regex; have you tried assigning the value of theEmail to a var before passing it to your function? I once tried chaining getDay() to Date()...didn't work out too well:

                var email = $("#theEmail").val();
		if (!regIsEmail(email)) {
				alert(email + " is not a valid email.");
				$("#theEmail").addClass('highs');
				$("#theEmail").focus();
				return false;
		} else $("#theEmail").removeClass('highs');

Open in new window

Also you might try using the regex directly, something I've had success with before if the RegExp object is giving me problems:

function regIsEmail(fData){ 
	return /^\w[0-9a-zA-Z.]+@[0-9a-zA-Z.]+\.[a-zA-Z]+$/.test(fData);
}

Open in new window

Alternatively, you could try reversing the test(but that would mean changing what is returned by your function):

return fData.match(reg);

Open in new window

0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39642628
I added the . into my previous expression and it seems to work:
var reg = new RegExp("^[0-9a-zA-Z.]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");

the expression was:
var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");

Anyway, it works.  I will try your suggestions to make it work better, but I'm going to mark this question as answered.  Thank you so much for your help putting me on the right track!
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39642784
Awesome! Glad I could help. :-)
0

Featured Post

Technology Partners: 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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

751 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