Web Analytics Made Easy -
StatCounter Email Regular Expression - CodingForum

Announcement

Collapse
No announcement yet.

Email Regular Expression

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

  • Email Regular Expression

    Hi,

    I have the following regular expression to validate email addresses:

    \w+([-+.]\w+)*@\w+([-.]\w+)*\.(com|co\.uk|net|org|info|biz|org\.uk|tv|at|be|cc|ws|ltd\.uk|plc\.uk|ac\.uk)

    It works fine for all email types listed in the above code apart from emails that end in '.org.uk'

    Its because the org\.uk is being interpreted as a js command g\.

    Does anyone know how to 'escape' this or any other technique that will allow the g to be interpreted as normal text?

    Much appreciated.


    __________________
    Paul Gladwell
    [email protected]

  • #2
    I have not tried it but if you put the g into square brackets it ought to fix it. [] normally encloses a range, such as [a-z] but
    it will doubtless work with one character.


    \w+([-+.]\w+)*@\w+([-.]\w+)*\.(com|co\.uk|net|org|info|biz|or[g]\.uk|tv|at|be|cc|ws|ltd\.uk|plc\.uk|ac\.uk)

    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


    • #3
      Thanks for the response but unfortunately that doesn't work.

      [email protected] is still an invalid format.

      Comment


      • #4
        The [g] doesn't work but any other letter will...

        Comment


        • #5
          Its fixed now. It was nothing to do with the g being mis-interpreted. It was failing because the test |org| was before |org\.uk| so it only tested on the 'org' part of the email address.

          I just moved the |org| test to the end.

          Comment


          • #6
            A tricky one!

            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


            • #7
              A more general solution would be to use ^ and $ so designate start and end of string, respectively. If you do that, the order of the choices becomes indifferent.

              How about national top domains? What if I have a .nu, .se, .de, .ru etc. email?
              liorean <[[email protected]]>
              Articles: RegEx evolt wsabstract , Named Arguments
              Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
              Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

              Comment

              Working...
              X