Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Validating Email with Regular Expression

Posted on 2013-11-12
5
Medium Priority
?
291 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

772 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