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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Alabama
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down Validating Emails

    Hello, I'm working on a simple (not so) code that makes sure that an email has:
    1. at least 3 characters
    2. an at symbol (@)
    3. again, at least 3 characters
    4. a dot (.)
    5. 3 or 4 characters

    Everything works just fine, until the part where the script checks that the last characters after the dot, don't exceed the quantity of four. Your can write an email address like "something@something.abcdefg" and it will take it when I told it not to!

    Here's the code

    Code:
    <script type="text/javascript">
    		
    		function checkEmail(){	
    		var toCheck = /[a-z0-9]{2,}\w@[a-z0-9]{3,}\.[a-z]{3}/gi;
    		var whatToCheck1 = document.emailForm.email.value;
    			if(toCheck.test(whatToCheck1) === true) {
    				alert("Email is valid");
    			}
    			else {
    			alert("Email is NOT valid");
    			}
    		}
    </script>

  • #2
    Regular Coder
    Join Date
    Jan 2005
    Posts
    221
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool

    this is the problem with a dfa/regex. the string is checked a character at a time, and as soon as something doesn't match it will give a false, but if all the parameters are met, it doesn't care what comes next... it still passes... i.e. DFA's can't count. there are good lecture slides on this at http://www-2.cs.cmu.edu/~15251/ go to calendar then the feb 8th lecture.

    try using an evaluator to find last index of dot and subtract from length however, this validator will exclude many valid addresses, such as

    foo.bar@baz.co.uk
    or
    jp@mypage.com
    ect.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function checkDomain(string){
    var email='invalid'
    if (string.length-string.indexOf('.')>2)
    email='valid'
    if (string.length-string.indexOf('.')>5)
    email='invalid'
    alert(email) //or use - return email;
    }

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    /^[a-z][-_.a-z0-9]*@([-a-z0-9]+\.)+[a-z]{2,6}$/i

    not perfect, but it will get you closer.

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no offence, but I often think that validating e-mail fields is a waste of time, since the address which goes in may not even exist. furthermore, it might be a valid email address but your script fails because you're not au fait enough with domains to have coded for it. for example, are you coding for .name and .museum domains?
    *keep it simple (TM)

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good point, we forgot to ask the OP what this was for. If it is for something like PHP List, the bad ones will get weeded out as bounces soon enough.

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's not just that. suppose you fill out the form perfectly, but it fails to validate only on the email address. of course, the address entered is correct, so that's not the problem. the problem is is the validation script is wrong because it fails to validate proper email addresses and as a result the form cannot be submitted. thus the user maybe cannot complete the shopping cart and therefore a sale is lost due to bad programming practices. if I ever came across a site like that, one which wouldn't validate a genuine email address, I'd never use it again. quite simply, bad coding is bad for business, and more-often-than-not email validation scripts are badly coded because they're hardwired to a particular configuration of domain names and email styles. therefore best to avoid IMHO.

    potential awkward email address:

    Code:
    another_person.another_company@server.a-very-long-email-address.com.uk
    or

    Code:
    another.person.another_company@server.a-very-long-email-address.uk.net
    see my point??
    *keep it simple (TM)

  • #8
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by jshl_wiz
    1. at least 3 characters
    2. an at symbol (@)
    3. again, at least 3 characters
    4. a dot (.)
    5. 3 or 4 characters
    What about user@bt.com or user@liverpoolfc.tv, etc…

  • #9
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Very good points, we still haven't heard back from the OP as to what this is for, it could make a huge difference. I see from some of the comments here that my above function needs to be amended, substituting lastIndexOf for indexOf. Then at least it should work on the last part of the domain portion of the address. I am assuming a valid domain ending is from 2 to 4 characters, inclusive. Is it not?

  • #10
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscheuer1
    I am assuming a valid domain ending is from 2 to 4 characters, inclusive. Is it not?
    .museum = 6
    .travel = 6
    *keep it simple (TM)

  • #11
    New Coder
    Join Date
    May 2005
    Location
    Alabama
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This script I'm going to use just for a "Write a comment about this site" form. The data is going to be stored in a CSV file (frontpage does this, not me) but I do not want people with addresses like just "asdfasfdasdf" which is logically not going to work. I know I made a mistake about the number of characters after the dot... but I can fix that can't I? So let's suppose that I put {2, 6} instead of {2,4}. The code still won't work.

    QUESTION What is the dollar sign "$" after the last "}" for? I've been reading about it but no book that I have explains it.

  • #12
    New Coder
    Join Date
    May 2005
    Location
    Alabama
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code seems to be working like this:

    Code:
    <script type="text/javascript">
    		
    		function checkEmail(){	
    		var toCheck = /[a-z0-9]{2,}\w@[a-z0-9]{2,}\.[a-z]{2,6}$/gi;
    		var whatToCheck1 = document.emailForm.email.value;
    			if(toCheck.test(whatToCheck1) === true) {
    				alert("Email is valid");
    			}
    			else {
    			alert("Email is NOT valid");
    			}
    		}
    		</script>
    But still, what's the dollar sign for? Does it tell the code to check the whole variable or what????

  • #13
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Take a look at liorean's sig has some good information on javascript regular expressions.

  • #14
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A trailing $ matches end-of-string, a leading ^ matches beginning-of-string. They're there to prevent prevent the RegExp from matching only part of an address.

    You do need the dash, underscore, and dot matching before the @, and you do need to be able to match multiple dots after.

  • #15
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your regular expression still assumes that all email addresses are name@domain.extention and that it might not include a subdomain or have underscores or dots in the username, like so:

    Code:
    jbot.javascript_coder@sub.domain.com
    do you see what I mean?

    you're also assuming that people are gonna put in proper email addresses and not just any old (albeit) validated rubbish, like so:

    Code:
    ahasjdhajdh@sdhajhdjahd.skjsdk
    another reasons for the futility of email validation is that the script must be maintained - you can't just fire-and-forget it because new domain extensions could easily invalidate your script. if your form submission can't be completed because it doesn't validate a simple email address (which more-often-than-not you'll get fake ones anyway because people don't like giving out their address willy-nilly), then it's gonna drive away a lot of users/potential customers.

    why is it so important to get people's email addresses anyway?

    <edit>see how this site renders an email address - it get's wrong, assuming you meant a four char extension and not one of the newer longer ones). lesson, never assume you know more than the user and what they're entering is not correct.</edit>
    Last edited by jbot; 07-01-2005 at 04:47 PM.
    *keep it simple (TM)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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