homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Validate e-mail address using JavaScript
Little nifty code you might find useful
DrDoc




msg:1487295
 4:07 pm on Apr 15, 2002 (gmt 0)

Here's a nifty JavaScript code you might find useful. It lets you validate an e-mail address in a form before it is submitted.

Put this script in the HEAD section:

<script language="JavaScript" type="text/javascript">
/*
Search for "document.yourform" and "document.yourform.email"
and replace with whatever matches the form on your page.
*/

<!--
function CheckEmail() {
document.yourform.email.value = document.yourform.email.value.toLowerCase();
var email = document.yourform.email.value;
var valid = "y";

if (email != "") {
if (email.length<7 ¦¦
email.indexOf("@.") != -1 ¦¦
email.indexOf("-.") != -1 ¦¦
email.indexOf("_.") != -1 ¦¦
email.indexOf("..") != -1 ¦¦
email.indexOf("._") != -1 ¦¦
email.indexOf(".-") != -1 ¦¦
email.indexOf(".@") != -1 ¦¦
email.indexOf("@-") != -1 ¦¦
email.indexOf("@_") != -1 ¦¦
email.indexOf("@") != email.lastIndexOf("@") ¦¦
email.indexOf("@") == -1 ¦¦
email.indexOf(".") == -1 ¦¦
(email.length - (email.lastIndexOf(".") + 1)) < 2) {
valid = "n";
alert("The e-mail address you entered is invalid.");
document.reg.mail.focus();
}
else {
a = "abcdefghijklmnopqrstuvwxyz0123456789@-_.";
b = 0;
while (b < email.length) {
if (a.indexOf(email.charAt(b)) == -1) {
valid = "n";
alert("The e-mail address may not contain \"" + email.charAt(b) + "\".");
document.yourform.email.focus();
b = email.length;
}
b = b + 1;
}
}
}
if (valid == "y") {
rx = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.[a-z][a-z]$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.aero$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.biz$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.coop$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.com$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.edu$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.gov$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.info$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.int$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.mil$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.museum$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.name$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.net$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.org$¦^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.pro$/i;
if (rx.test(email)) {
rx = /^[a-z0-9_]/i;
if(!(rx.test(email))) {
valid = "n";
alert("The e-mail address you entered is invalid.");
document.yourform.email.focus();
}
}
else {
valid = "n";
alert("The e-mail address you entered is invalid.");
document.yourform.email.focus();
}
}
if (valid == "y") {
document.yourform.submit();
}
}
//-->
</script>

Instead of a submit button, use TYPE="Button" and add onClick="CheckEmail()" to the tag.

<added>
Note that the looooooong
"rx = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9] ... [a-z0-9\-\.]*[a-z0-9]\.pro$/i;"
statement has to be on the same line!
</added>

(edited by: DrDoc at 4:10 pm (utc) on April 15, 2002)

 

rpking




msg:1487296
 4:09 pm on Apr 15, 2002 (gmt 0)

surely a regular expression would be a bit more condensed...

DrDoc




msg:1487297
 4:13 pm on Apr 15, 2002 (gmt 0)

Well, it COULD be more condensed if you left out the .com .edu .net (etc) check, and only made sure that the domain name extension is anywhere between 2 and 6 characters long.

But, that would enable for e-mail addresses like whatever@domain.asdhka

DrDoc




msg:1487298
 4:14 pm on Apr 15, 2002 (gmt 0)

The first rx would then look like this:

rx = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.[a-z]{2,6}$/i;

joshie76




msg:1487299
 4:39 pm on Apr 15, 2002 (gmt 0)

A little e-mail validation is worth it though I think that might be excessive. Most just look for an '@' and a '.'.

If someone has e-mail validation and I don't want to give my e-mail I just type nonsense@nonsense.com - there's no way you can check if that's real (in Javascript anyway).

I'm not alone in doing this, well, either I'm not alone in doing this or bill.gates@microsoft.com and mickey.mouse@disney.com are regular visitors to our site!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved