SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Email address validation not working :(

    Hi,

    I am making a script to validate email addresses.

    I first used a regular expression.

    Then, I checked if two or more symbols are next to each other using loops. However, my code doesn't work.

    Please help me out.

    Thanks in advance!!!

    HTML Code:
    <html>
    <head>
    <title>Validating email addresses using JavaScript</title>
    <script type="text/javascript">
    function validate()
    {
    var email = new RegExp("^[A-Z0-9a-z]{1}[A-Z0-9a-z|\._]*[A-Z0-9a-z]{1}[@]{1}[A-Z0-9a-z]{1}[A-Z0-9a-z-\.]*[A-Z0-9a-z]{1}[\.]{1}[A-Za-z]{3,4}$","i");
    var given = document.getElementById("email").value;
    document.getElementById("email").value.toLowerCase();
    var result = email.test(given);
    	if (result==false)
    	{
    	return false;
    	}
    	else
    	{
    	var num =0;
    		while (num<(given.length)-1 && num!=-1)
    		{
    		num = given.indexOf(".",num)
    			if (num>0 && (num<(given.length)-1))
    			{
    				if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@" || given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
    				{
    				return false;
    				}
    			}
    			else if (num==0)
    			{
    				if (given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
    				{
    				return false;
    				}
    			}
    			else if (num==(given.length)-1)
    			{
    				if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@")
    				{
    				return false;
    				}
    			}
    			if (num<(given.length)-1 && num!=-1)
    			{
    			num=num+1;
    			}
    		}
    		if (given.indexof("-",0)!=-1)
    		{
    		num =0;
    			while (num<(given.length)-1 && num!=-1)
    			{
    			num = given.indexOf("_",num)
    				if (num>0 && (num<(given.length)-1))
    				{
    					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@" || given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
    					{
    					return false;
    					}
    				}
    				else if (num==0)
    				{
    					if (given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
    					{
    					return false;
    					}
    				}
    				else if (num==(given.length)-1)
    				{
    					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@")
    					{
    					return false;
    					}
    				}
    				if (num<(given.length)-1 && num!=-1)
    				{
    				num=num+1;
    				}
    			}	
    		}
    		if (given.indexOf("_",0)!=-1)
    		{
    		num =0;
    			while (num<(given.length)-1 && num!=-1)
    			{
    			num = given.indexOf("-",num)
    				if (num>0 && (num<(given.length)-1))
    				{
    					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@" || given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
    					{
    					return false;
    					}
    				}
    				else if (num==0)
    				{
    					if (given.charAt(num+1)=="." || given.charAt(num+1)=="-" || given.charAt(num+1)=="_" || given.charAt(num+1)=="@")
    					{
    					return false;
    					}
    				}
    				else if (num==(given.length)-1)
    				{
    					if (given.charAt(num-1)=="." || given.charAt(num-1)=="-" || given.charAt(num-1)=="_" || given.charAt(num-1)=="@")
    					{
    					return false;
    					}
    				}
    				if (num<(given.length)-1 && num!=-1)
    				{
    				num=num+1;
    				}
    			}	
    		}
    	}	
    }
    </script>
    </head>
    <body>
    <form method="post" action="doesnotexists.php" onsubmit="return validate()">
    <input type="text" name="email"><br />
    <input type="submit" value="Check">
    </form>
    </body>
    </html>[HIGHLIGHT="HTML4Strict"][/HIGHLIGHT]

  2. #2
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function validateEmail(email) 
    { 
     var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
    ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
    -Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
     return email.match(re) 
    }
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by smftre View Post
    Code:
    function validateEmail(email) 
    { 
     var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
    ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
    -Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
     return email.match(re) 
    }
    Thanx 4 ur reply!

    However, it didn't work out.

    Please see if you can help me out.

    Actually I've written a 1 line snippet that works in PHP, so I think that JavaScript doesn't support PCRE(Perl Compatible Regular Expressions), as PHP does.

  4. #4
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about this?

    Code:
    function validateEmail(email) {
        var mail_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if( mail_filter.test(email) ){
            // valid
        } else {
            // invalid
        }
    }
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  5. #5
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by smftre View Post
    how about this?

    Code:
    function validateEmail(email) {
        var mail_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if( mail_filter.test(email) ){
            // valid
        } else {
            // invalid
        }
    }
    Thanx again!

    But this also accepts 'sanchit@-algo.-csail.-mit.edu' & 'sanchit@algo.-csail.-mit.edu',etc.

    I found some errors in my file - I had written 'name' instead of 'id' & had missed 3 ';'s.

    Now, I have fixed it.

    However, you taught me a very great thing - I had been wrongly taught that JavaScript regular expressions are not PCRE. I saw grouping of units in your code & that prompted me to develop my own regular expression using grouping -

    Code:
    ^[a-z0-9A-Z]{1,}[\._]{0,1}[a-z0-9A-Z]{0,}){1,}[a-z0-9A-Z]{1}[@]([a-z0-9A-Z]{1,}[\._\-]{0,1}[a-z0-9A-Z]{0,}){1,}[a-z0-9A-Z]{1}[\.]{1}[a-zA-Z]{2,4}$
    It works for most real life examples(that I tested now; please tell me in case I am wrong). So I'll use it instead of those long loops.

    Thanks again!

  6. #6
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I think you are onto something there!
    Let me know if you have any real-world issues with it going forward!
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  7. #7
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by smftre View Post
    Yeah, I think you are onto something there!
    Let me know if you have any real-world issues with it going forward!
    Thanks!


Tags for this Thread

Bookmarks

Posting Permissions

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