PDA

View Full Version : email validation


reigalz
11-01-2002, 09:04 AM
hi, what's the best method i can use to check whether a user has entered a valid email address using javascript? thanks!

zoobie
11-01-2002, 09:12 AM
The best method actually sends an email to that address and, if it actually exists, validates. This takes a little time (20 seconds).

Other than that, checking for invalid characters, a @, and a dot is probably your second best bet.

%^#*zoobie@yahoo.com :D

reigalz
11-01-2002, 10:16 AM
Originally posted by zoobie
The best method actually sends an email to that address and, if it actually exists, validates. This takes a little time (20 seconds).


how do i send send an email to that address and know that it exists? for your infomation, i'm using java language and javascript in jsp. thanks :D

WA
11-01-2002, 10:36 AM
For all intent and purposes, a solid email validating JavaScript ought to suffice: http://www.javascriptkit.com/script/script2/acheck.shtml

WA
11-01-2002, 10:36 AM
For all intent and purposes, a solid email validating JavaScript ought to suffice: http://www.javascriptkit.com/script/script2/acheck.shtml

zoobie
11-01-2002, 10:37 AM
It's a server side script...probably php if my memory serves me correctly. Forget exactly where I saw it...

Most just use javascript ones like here (http://www.hotscripts.com/JavaScript/Scripts_and_Programs/Forms/).

Sorry I can't be of more help...:D

WA
11-01-2002, 10:37 AM
Well, as reigalz did ask "the best method using JavaScript", here's a solid email validation JS script: http://www.javascriptkit.com/script/script2/acheck.shtml

dominicall
11-01-2002, 11:53 AM
Hi

This is the script that I use having tested many and IMHO is the most strict test without sending an email itself.


// Check that an email address has the form something@something.something
function isValidEmailStrict(address)
// This is the new email check I got from javascript examples
//(http://www.js-examples.com/example/?ex=946&mode=1&COLOR_OFF=YES)
{
var checkTLD=0;
// edit the list of TLDs below - I had to remove some to fit here - Dominic
var knownDomsPat=/ ^(com|net|org|int|mil|gov|biz|aero|name|uk)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=address.match(emailPat);

//check there is an email address
if (matchArray==null) {
return false;
}
//check the username doesn't contain invalid characters
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
return false;
}
}
//check the domain name doesn't contain invalid characters
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
return false;
}
}
//check username is not invalid
if (user.match(userPat)==null) {
return false;
}
//check destination IP is not invalid if email is IP address format
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray>255) {
return false;
}
}
}
//check domain name is not invalid
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
return false;
}
}
//check TLD is not invalid
if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
return false;
}
if (len<2) {
return false;
}
//if we get this far everything is OK so we return true
return true;
}



There are a number of (IIS) server components that will do an MX record check and actually check whether the complete email address exists - but they do take extra time.

I'm about to start evaluating ServerObjects ASPMX for doing this - will post back my results.

Dominic

reigalz
11-02-2002, 12:53 PM
thanks for all the help! i will go and try it out :thumbsup:

reigalz
11-05-2002, 01:59 AM
i've seen the code from http://www.javascriptkit.com/script/script2/acheck.shtml

but there's some part of the code which i dun understand. can somebody explain to me? thanks!!

here's the code:

<form name="validation" onSubmit="return checkbae()">
Please input a valid email address:<br>
<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit">
</form>

<script language="JavaScript1.2">

var testresults

function checkemail(){
var str=document.validation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}

</script>

<script>
function checkbae(){
if (document.layers||document.getElementById||document.all)
return checkemail()
else
return true
}
</script>

may i noe wat's the use of the function checkbae()? wat is if (document.layers||document.getElementById||document.all) ?
can i exclude the checkbae() function?

JustAsking
11-05-2002, 02:16 AM
document.getElementById - is used by IE5.0 + and NN6.0 + to store objects. This stores the objects in a tree structure.

document.all - is needed for IE 4.0 to sotre objects rather than in the a tree structure as above.

document.layers - is needed for NN4.0 to sotre objects.

Therefore, to answer your question, if you want your script to be cross-browser compatible then you need to leave this function intact.

reigalz
11-05-2002, 02:19 AM
but if i exclude the function checkbae(), will there be any problem?

JustAsking
11-05-2002, 02:25 AM
reigalz,

Perhaps YOU should test to see whether it works but excluding the function. Anyway, here is a working script you can use without the checkbae() function.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<form name="validation" onSubmit="return checkemail()">
Please input a valid email address:<br>
<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit">
</form>

<script language="JavaScript1.2">

var testresults

function checkemail(){
var str=document.validation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}

</script>
</html>

reigalz
11-05-2002, 02:48 AM
ok, i'll try...thanks alot!! ;)

zeta
11-09-2002, 03:20 PM
I tried the popular email validation script provided above:

http://www.javascriptkit.com/script/script2/acheck.shtml

I copied the entire code into my page but I get an invalid email alert for valid addresses such as "jim@aol.com". What am I missing?

victoria_1018
11-13-2002, 02:22 AM
You can try using this method, it works on my script.

<script language="JavaScript">
<!--
function VerifyData(form)
{

if (form.email.value == "")
{
alert("Please enter a value in the Email input field.");
form.email.focus();
return false;
}

specialChars=";:/";
for(a=0;a<specialChars.length;a++)
{
c=specialChars.charAt(a);
if(form.email.value.indexOf(c,0)!=-1)
{
alert("Please enter a valid email address.");
form.email.focus();
return false;
}
}
emailAt=form.email.value.indexOf("@",0);
emailDot=form.email.value.indexOf(".",0);
if(emailAt == -1 || emailDot == -1)
{
alert("Please enter a valid email address.");
form.email.focus();
return false;
}

}
-->
</script>

Regards

glenngv
11-13-2002, 06:57 AM
you sure this would work on your script?

@.blah

obviously that isn't a valid email address but i think it will pass on your validation :D