Solved

Validating Email with Regular Expression

Posted on 2013-11-12
5
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome! Glad I could help. :-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

743 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