SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript validation for email address

    what is the javascript validation for the email address like
    php.ycho@gmail.com

    i used the simple validation like
    "^\\w{1,}@\\w{1,}(\\.\\w{1,}){1,}$"
    which works well for php@gmail.com but doesnot work for php.ycho@gmail.com
    can anybody make a modification to above pattern to work for php.ycho@gmail.com
    Thanks in advance to all of you

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just as a small note to remember that if the user has javascript turned off then the email address will not be validated, its better to use a server side language to do this.
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually i dont have so much knowledge in regex....
    and i have to do the work withing hour...and i thought forum is the best place..

    Please help me in such case..
    i appreciate for any help

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    I generally use

    Code:
    emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
    which accepts the most commonly used formats of email addresses but doesn't cater for the following two styles of email address

    "my email"@somewhere.com
    me@[10.2.56.100]

    I usually use the same expression in both Javascript and PHP.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast rusagar's Avatar
    Join Date
    Jan 2007
    Location
    Kathmandu, Nepal
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this one :

    var goodEmail = form.txtemail.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

    if (!goodEmail)
    {
    alert("The Email address you entered is invalid please try again!")
    form.txtemail.focus()
    return (false);
    }

  6. #6
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your regexp not work for national domains, I use this one
    Code:
    ^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$
    but it will not works for museum, I didnt know that such domain exist.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    museum is the longest of the current top level domain names - all of the others are 2, 3, or 4 characters with the 2 character ones being country specific (eg. .tv for Tuvala) and the others being the generic ones I included in my expression above.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The regex I use is fairly generic:
    /^.+@[a-z0-9]+([_\.\-]{0,1}[a-z0-9]+)*([\.]{1}[a-z0-9]+)+$/

    As far as I'm aware, the name before the @ sign can be almost anything - even a single space character.


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
  •