Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Email Validator

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Email Validator

    Email Validator JavaScript (with and without RegExps):

    Code:
    <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.co...-validator.htm


  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Northern California
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thats Pretty Handy! If I used reg exp I could Cut My Code In Half!

  • #3
    New Coder
    Join Date
    Jul 2003
    Location
    The World Wide Web
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry if I am being ignorant, but what is the difference between that and this:
    PHP Code:
    <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> 
    There is always some madness in love. But there is also always some reason in madness....

    [:: World-Craft :-: ::]

  • #4
    New to the CF scene
    Join Date
    Aug 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hello :(

    "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.
    Last edited by abokcomputers; 08-08-2003 at 08:17 AM.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    abokcomputers there's no need for that kind of rabid abuse, thank you. Please be polite and constructive in your critisisms.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #7
    New to the CF scene
    Join Date
    Aug 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OkeOkey, sorry about that last message before, i the wheder was bad

    Okey, anyway, u should add mail rule that if server = hot.ee 4 example, then -_- is okey, but 0r s0mthing 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 )

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

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

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually - I think the username is the more controversional portion of your email than the domain.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #9
    New to the CF scene
    Join Date
    Aug 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ;)

    Thats what i talking about

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    function isEmail(email)
    {
        return /^
    w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/.test(email);

    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •