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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts

    basic validation of email address

    I have been playing around with this for a few weeks on and off but can not figure this out.

    I am after a very basic, works all the time email validator.

    Every email starts with some character/s (maybe a DOT somewhere) then @ then some more character/s then a DOT then some more characters. But I can not seems to get this to work right.

    Is there an easier way ?

    Code:
    function checkEmailValid(email) {
    	f = email.indexOf('.');
    	s = email.indexOf(' ');
    	a = email.indexOf('@');
    	lf = email.lastIndexOf('.');
    	la = email.lastIndexOf('@');
    //	alert('f ' + f + '\n s ' + s + '\n a ' + a + '\n lf ' + lf + '\n la ' + la);
    		if (email != ''			&&
    			s == -1			&&
    			(f == lf || f < a)	&&
    			a				&&
    			a == la			&&
    			lf > a			&&
    			lf < email.length) {
    			return true;
    		} else {
    			return false;
    		}
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    That is a completely obsolete way of validating an email address. Prefer to use a regular expression:-

    Code:
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email.value))) {   //  returns true if invalid
    If you want to take account of domains .museum and .travel change {2,4} to {2,6}.

    Of course, that does not ensure that the email address entered by the user is actually correct. There may be spelling errors. me@gmial.com You can improve the chances of that by requiring that the email address is entered twice, and check that the two match.

    For a working example of what you are trying for see:-

    http://www.qualitycodes.com/tip/4/va...xpression.html

    But that allows @@

    There is no point in re-inventing the wheel but making it oval this time! Both Google and this forum have many examples.


    Quizmaster: What are the first and last names of the Duchess of Cambridge?
    Contestant: Elizabeth Taylor
    Last edited by Philip M; 08-17-2013 at 12:14 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    yes, there are lots to choose from, but having one that works for all is what i would really like. I do not mind if the email is not a real one, 235d3tg@sjeud.cpw

    just so it has what it should have the @ and a .

    no matter what characters are before the @ (even if it is 123.456@789.654, or 654.ert@76531.sorud) then more characters then a DOT then more characters. But as long as it only has one @

    as long as it is formatted like an email should be then this would do.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Well, I have given you the regular expression which works for 99.99% of addresses.

    Another example of the older crude version is at http://www.texaswebdevelopers.com/ex...validation.asp
    The regex is much simpler and better.
    Last edited by Philip M; 08-17-2013 at 01:34 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by Philip M View Post
    If you want to take account of domains .museum and .travel change {2,4} to {2,6}.
    Don't forget all of the upcoming new gTLDs at https://gtldresult.icann.org/applica...licationstatus. It would be unfortunate if someone were unable to enter their *@domain.afamilycompany, *@domain.vermögensberater, or *@domain.youtube email addresses.

    Quote Originally Posted by needsomehelp View Post
    yes, there are lots to choose from, but having one that works for all is what i would really like. I do not mind if the email is not a real one, 235d3tg@sjeud.cpw

    just so it has what it should have the @ and a .

    no matter what characters are before the @ (even if it is 123.456@789.654, or 654.ert@76531.sorud) then more characters then a DOT then more characters. But as long as it only has one @

    as long as it is formatted like an email should be then this would do.
    Here's a simple validation script that covers most cases including the gTLD cases I just mentioned. It should allow every email format that doesn't involve quotation marks (like "email address"@domain.tld, which is technically a valid format though not used).

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    		<style>
    			label[for]::after { content: ":"; }
    			.valid { background-color: lime; color: black; }
    			.invalid { background-color: red; color: white; }
    		</style>
    		<script>
    			(function () {
    				"use strict";
    				function validate_email_address(button_activation_event) {
    					// Regular Expression Explanation: Zero or more instances of “abc.” followed by one instance of “abc@” followed by zero or more instances of “abc.” followed by one instance of “abc.abc” where “abc” is a string consisting of one or more characters that are not the characters COMMERCIAL AT (“@”), FULL STOP (“.”), or SPACE (“ ”).
    					var email_format = /^([^@\. ]+\.)*[^@\. ]+@([^@\. ]+\.)*[^@\. ]+\.[^@\. ]+$/;
    					var email_address_field = document.getElementById("email_address");
    					var email_address = email_address_field.value;
    					button_activation_event.preventDefault();
    					if (email_format.test(email_address)) {
    						email_address_field.setAttribute("class", "valid");
    					}
    					else {
    						email_address_field.setAttribute("class", "invalid");
    					}
    				}
    				function initialize_script() {
    					var button = document.getElementById("button");
    					button.addEventListener("click", validate_email_address);
    				}
    				document.addEventListener("DOMContentLoaded", initialize_script);
    			})();
    		</script>
    	</head>
    	<body>
    		<form>
    			<label for="email_address">Email Address</label> <input id="email_address" type="text">
    			<button id="button">Validate</button>
    		</form>
    	</body>
    </html>
    Last edited by Arbitrator; 08-18-2013 at 06:27 PM. Reason: I fixed several minor grammatical errors and a minor regex issue.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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