Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: open

Message Too Old, No Replies

Validate e-mail address using JavaScript

Little nifty code you might find useful

     

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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!

 

Featured Threads

Hot Threads This Week

Hot Threads This Month