Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


String.isEmail

Flash Version: FLASH MX
Author: jonas@onrelease.org
Total downloads: 15952
Category: String
Publish Date: 2003-03-27 01:33:16

the code:
// String.isEmail Method v2.0
// by Jonas Galvez (jonas@onrelease.org)

String.prototype.isEmail = function() {
    var ref = arguments.callee;
    if(this.indexOf("@") == -1) return false;
    if(!isNaN(this.charAt(0))) return false;
    var email, user, domain, user_dots, domain_dots;
    if((email = this.split("@")).length == 2) { 
        if((domain = email[1]).split(".").pop().length > 3) return false;

   // update 27 Mar 2003 
   // David Pérez Ortuño (david@ideas-shop.com)       
   if(domain.split(".").length < 2) return false;
   // end update
       
       
        if((user = email[0]).indexOf(".") && domain.indexOf(".")) {     
      
            if(domain.lastIndexOf(".") > domain.length-3) return false;
      
            for(var c, t, i = (user_dots = user.split(".")).length; i--;) {
                c = user_dots[i]; t = !ref.$_text.call(c, "-", ".", "_");
                if(t || !isNaN(c)) return false;
            };
            for(var c, t, i = (domain_dots = domain.split(".")).length; i--;) {
                c = domain_dots[i]; t = !ref.$_text.call(c, "-", ".");
                if(t || !isNaN(c)) return false;
            };
        } else return false;
    } else return false;
    return true;
};
String.prototype.isEmail.$_punctuation = function() {
    if(this == "") return false;
    for(var i = arguments.length; i--;) {
        if(this.indexOf(arguments[i]) == 0) return false;
        if(this.indexOf(arguments[i]) == this.length-1) return false;
    };
    return true;
};
String.prototype.isEmail.$_text = function() {
    var ref = arguments.caller;
    if(!ref.$_punctuation.apply(this, arguments)) return false;
    var others = arguments; var checkOthers = function(str) {
        for(var i = others.length; i--;) if(str == others[i]) return true;
        return false;
    };
    for(var c, alpha, num, i = this.length; i--;) {
        c = this.charAt(i).toLowerCase();
        alpha = (c <= "z") && (c >= "a");
        num = (c <= "9") && (c >= "0");
        if(!alpha && !num && !checkOthers(c)) return false;
    };
    return true;
}; 
ASSetPropFlags(String.prototype, "isEmail", 1);

Comments (4)

nusterboker on 25th April 2005

usage?

ryanguill on 19th March 2005

sorry for my ignorance, but doesnt actionscript have some sort of regex implementation? If not then this is an awesome function. I may still convert it to cf as im never sure of my regex skills

david on 27th March 2003

It returns true with things like david@ide because it did not check the minimum number of components for the domain.

I solved it adding to the process the following condition

if(domain.split(".").length < 2) return false;


Thank you Jonas!

alessandro on 27th January 2003

really great job jonas!