Solved

Validating Email with Regular Expression

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery Validation Messages Not Showing Up 3 23
Asp in script 6 39
CORS Error from Chrome, but FireFox is OK 1 85
Head to not include on scroll 4 23
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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

830 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