Solved

Validating Email with Regular Expression

Posted on 2013-11-12
5
266 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

914 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

14 Experts available now in Live!

Get 1:1 Help Now