PDA

View Full Version : Email Validator


premshree
07-30-2003, 03:45 PM
Email Validator JavaScript (with and without RegExps):


<script language="JavaScript">
//////////////////////////////////////////////////
// <Email Validator> //
// by Premshree Pillai //
// http://www.qiksearch.com //
// http://premshree.resource-locator.com //
//////////////////////////////////////////////////

/* Without RegExps */
function isEmail(who) {
function isEmpty(who) {
var testArr=who.split("");
if(testArr.length==0)
return true;
var toggle=0;
for(var i=0; i<testArr.length; i++) {
if(testArr[i]==" ") {
toggle=1;
break;
}
}
if(toggle)
return true;
return false;
}

function isValid(who) {
var invalidChars=new Array("~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?");
var testArr=who.split("");
for(var i=0; i<testArr.length; i++) {
for(var j=0; j<invalidChars.length; j++) {
if(testArr[i]==invalidChars[j]) {
return false;
}
}
}
return true;
}

function isfl(who) {
var invalidChars=new Array("-","_",".");
var testArr=who.split("");
which=0;
for(var i=0; i<2; i++) {
for(var j=0; j<invalidChars.length; j++) {
if(testArr[which]==invalidChars[j]) {
return false;
}
}
which=testArr.length-1;
}
return true;
}

function isDomain(who) {
var invalidChars=new Array("-","_",".");
var testArr=who.split("");
if(testArr.length<2||testArr.length>4) {
return false;
}
for(var i=0; i<testArr.length; i++) {
for(var j=0; j<invalidChars.length; j++) {
if(testArr[i]==invalidChars[j]) {
return false;
}
}
}
return true;
}


var testArr=who.split("@");
if(testArr.length<=1||testArr.length>2) {
return false;
}
else {
if(isValid(testArr[0])&&isfl(testArr[0])&&isValid(testArr[1])) {
if(!isEmpty(testArr[testArr.length-1])&&!isEmpty(testArr[0])) {
var testArr2=testArr[testArr.length-1].split(".");
if(testArr2.length>=2) {
var toggle=1;
for(var i=0; i<testArr2.length; i++) {
if(isEmpty(testArr2[i])||!isfl(testArr2[i])) {
toggle=0;
break;
}
}
if(toggle&&isDomain(testArr2[testArr2.length-1]))
return true;
return false;
}
return false;
}
}
}
}

/* With RegExp */
function isEmail2(who) {
var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
return(email.test(who));
}
</script>


See example at http://premshree.resource-locator.com/javascripts/email-validator.htm

:thumbsup:

scriptkeeper
07-31-2003, 01:24 AM
Thats Pretty Handy! If I used reg exp I could Cut My Code In Half!

World-Craft
07-31-2003, 02:07 PM
sorry if I am being ignorant, but what is the difference between that and this:

<script>
function parseMail(str) {
var reg1 = /(@.*@)|(..)|(@.)|(.@)|(^.)/; // not valid
var reg2 = /^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$/; // valid
if (!reg1.test(str) && reg2.test(str)) return true;
return false;
}
</script>
<form method="post" action="" onsubmit="return false">
<fieldset><label for="email">Email:</label> <input type="text" id="email" />
<input type="button" onclick="alert(parseMail(this.form.email.value))" value="Check it" /></fieldset>
</form>

abokcomputers
08-06-2003, 01:40 PM
"sorry about the last mssage"

You script dosent realy w0rk like its sposto, ther is lotof more tools available what reali looking into server if this email exist or not.

4 example my email -_-@hot.ee your script says that its wrong... But actualy its not.

brothercake
08-06-2003, 02:10 PM
abokcomputers there's no need for that kind of rabid abuse, thank you. Please be polite and constructive in your critisisms.

premshree
08-06-2003, 04:22 PM
Well, you are right, that won't validate against the script...most email providers won't allow an email ID like that and that's what I have assumed...

abokcomputers
08-08-2003, 08:24 AM
OkeOkey, sorry about that last message before, i the wheder was bad:D

Okey, anyway, u should add mail rule that if server = hot.ee 4 example, then -_- is okey, but 0r s0mthing:D i'm always pissd0ff if im start to register sometimes s0mthing in net, and then deem email validator telling me that email dosen't exizt... whay they just dont send validation link to the email, and all should be okey :o)

[-- most email providers won't allow an email ID like that - actualy mayn dos't allow eighter, but i make it to allow :D --]

If someone want i can register for ya some pop3 emails from hot.ee server whit multi email boxis (inside one box 5 boxis) :D

beetle
08-15-2003, 10:36 PM
Actually - I think the username is the more controversional portion of your email than the domain.

abokcomputers
08-18-2003, 04:44 AM
Thats what i talking about :)

whammy
08-19-2003, 04:05 PM
I use a much looser email regex - which would also reject that. I don't think that conforms to the RFC - but I could be wrong. Anyway, I do it in one line:

function isEmail(email)
{
return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email);
}