Web Analytics Made Easy -
StatCounter Validating an Email Address - CodingForum

Announcement

Collapse
No announcement yet.

Validating an Email Address

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validating an Email Address

    Hi there,

    Can someone explain this piece of code to me? I get the general idea here but can you break it down for me as I don't just want to copy and paste it without fully understanding it!

    Thank you so much in advance!

    Here it is:


    /^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,5}$/i

  • #2
    Also,

    Can you tell me what this means?

    /^\s*$/

    Comment


    • #3
      OK, its hard to work out which is the best way to explain in this medium but I'll have a go.

      I'll work from left to right.

      ok, then;

      the / is the first boundary of the regex.
      the ^ means the beginning of your regex manipulation
      the first [] section shows what chars are allowed in the first part of the email address.
      the + means that each of those chars can occur more than once
      the @ symbol represents the @ in the address
      the second [] section shows the chars allowed after the @ symbol and before the .tld
      again, the + means more than one occurence is accepted
      \ means escape the .
      [A-Z] allows for all alphabetical chars for the domain suffix (tld)
      {2,5} means minimum of 2 max of 5 chars in the tld
      $ means the end of your manipulation
      / denotes the end of the regex and
      i means case insensitive

      It should be noted that using the ^ and $ means you are checking the complete snippet. if you just use // and no ^ or $, then you are chekcing for a match anywhere, whereas the ^ and $ means that the match has to match the whole thing from start to end. I'm sure there is an easier way to explain that but I hope that helps

      the '5' needs to be changed to about 7 since tld's can have more than 5 letters. not sure the max number though. Please note I am not evaluating whther that is in fact a good regex for your purpose. O'Reilly does a good book on the subject (and others) at your fav online bookshop.

      ps, are you from Emerald Isle?
      bazz
      Last edited by bazz; Apr 15, 2009, 07:52 PM.
      "The day you stop learning is the day you become obsolete"! - my late Dad.

      Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
      Useful MySQL resource
      Useful MySQL link

      Comment


      • #4
        Not really a JavaScript man, but wouldn't that fail when presented with emails whose issuing website is registered under a third-level domain (e.g. *.co.uk)?
        Blog | Twitter
        Useful links: W3C HTML Validator | W3C CSS Validator | HTML 5 Guide
        CF: HTML & CSS Resources/Tutorials Thread | HTML & CSS Posting Rules and Guidelines
        Remember: no link, no code, no help!

        Comment


        • #5
          Originally posted by bazz View Post
          OK, its hard to work out which is the best way to explain in this medium but I'll have a go.

          I'll work from left to right.

          ok, then;

          the / is the first boundary of the regex.
          the ^ means the beginning of your regex manipulation
          the first [] section shows what chars are allowed in the first part of the email address.
          the + means that each of those chars can occur more than once
          the @ symbol represents the @ in the address
          the second [] section shows the chars allowed after the @ symbol and before the .tld
          again, the + means more than one occurence is accepted
          \ means escape the .
          [A-Z] allows for all alphabetical chars for the domain suffix (tld)
          {2,5} means minimum of 2 max of 5 chars in the tld
          $ means the end of your manipulation
          / denotes the end of the regex and
          i means case insensitive

          It should be noted that using the ^ and $ means you are checking the complete snippet. if you just use // and no ^ or $, then you are chekcing for a match anywhere, whereas the ^ and $ means that the match has to match the whole thing from start to end. I'm sure there is an easier way to explain that but I hope that helps

          the '5' needs to be changed to about 7 since tld's can have more than 5 letters. not sure the max number though. Please note I am not evaluating whther that is in fact a good regex for your purpose. O'Reilly does a good book on the subject (and others) at your fav online bookshop.

          ps, are you from Emerald Isle?
          bazz
          Can I save that on a web page for easy reference? I just wanted to ask first in case you wanted to make tutorial page. That's a sweet explanation

          Comment


          • #6
            Originally posted by CyanLight View Post
            Not really a JavaScript man, but wouldn't that fail when presented with emails whose issuing website is registered under a third-level domain (e.g. *.co.uk)?
            yeh I think it would.

            I doubt that it is a very good regex. I would need to hoke out the O'reilly book but I seem to recall the example they gave is much longer and more difficult to read.


            @TinyScript: I don't see a problem with you copying it. No need to attribute it to me either.

            bazz
            "The day you stop learning is the day you become obsolete"! - my late Dad.

            Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
            Useful MySQL resource
            Useful MySQL link

            Comment


            • #7
              I find the best email regex is:-

              if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(eMailAddress.value))) {


              Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

              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.

              Comment


              • #8
                yeh, Philip, thats more like the O'Reilly one I referred to though I can't place the book.

                re. your sig.

                Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
                I can relate to that

                bazz
                "The day you stop learning is the day you become obsolete"! - my late Dad.

                Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
                Useful MySQL resource
                Useful MySQL link

                Comment

                Working...
                X