SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate for proper email format

    Hi all,

    I've got the code below, in my page for a form, but it just checks to see if a field is blank or not. Is there an easy and quick way to check for the proper email format as well please ? Not looking for anything more complicated than that, just the correct email format, not whether the email address exists for example.

    Any help very much appreciated.

    Dez.

    function validate() {

    if (document.email.value.length==0) {
    alert("please enter your email address");
    return false;
    }
    if (document.form.name.value.length==0) {
    alert("please enter your name");
    return false;
    }

    document.form.submit()
    return true;
    }

  2. #2
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    just googled E-mail validation and got this: http://javascript.internet.com/forms...n---basic.html

  3. #3
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for that, it is appreciated. I did Google it, but must have missed that one.

    One thing, I'm new to the email validation thing - is the code that I'm using Javascript ? If so, does anyone know of an easy, quick way to do the same thing, but without Javascript please ?

    Any help much appreciated.

    Dez.

  4. #4
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My email regex:
    Code:
    var EmailRegExp = new
    RegExp(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
    z0-9]|(-[A-Za-z0-9]))+)\.([A-Za-z]{2,6})(\.([A-Za-z]{2}))?$/);
    Yes, your code is JavaScript. What other language would you want to use instead (there aren't any alternatives if you want to do the validation client-side)?

  5. #5
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Buddy Bradley View Post
    My email regex:
    Code:
    var EmailRegExp = new
    RegExp(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
    z0-9]|(-[A-Za-z0-9]))+)\.([A-Za-z]{2,6})(\.([A-Za-z]{2}))?$/);
    Yes, your code is JavaScript. What other language would you want to use instead (there aren't any alternatives if you want to do the validation client-side)?
    Many thanks Bradley, that's appreciated.

    Thanks also for the extra info - I am able to use php with my hosts if that can be done in a quick way.

    Dez.

  6. #6
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With PHP, the form would have to be submitted to the server, validated, then if there were errors it would then be returned to the original page and present the user with the details of their error. It's more work than the JavaScript-only version (although you should really have both).

  7. #7
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Buddy Bradley View Post
    With PHP, the form would have to be submitted to the server, validated, then if there were errors it would then be returned to the original page and present the user with the details of their error. It's more work than the JavaScript-only version (although you should really have both).
    Many thanks again Bradley.

    If you had php on it, why would you also need js as well ? I thought php would do it all ?

    Also, how would I combine the code below :

    function validate() {

    if (document.email.value.length==0) {
    alert("please enter your email address");
    return false;
    }
    if (document.form.name.value.length==0) {
    alert("please enter your name");
    return false;
    }

    document.form.submit()
    return true;
    }

    With the code below please :

    Dez.


    Code:
    var EmailRegExp = new
    RegExp(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
    z0-9]|(-[A-Za-z0-9]))+)\.([A-Za-z]{2,6})(\.([A-Za-z]{2}))?$/);

  8. #8
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    email validation patterns !
    pick your choice ...
    http://regexlib.com/Search.aspx?k=email

  9. #9
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dez View Post
    Many thanks again Bradley.

    If you had php on it, why would you also need js as well ? I thought php would do it all ?
    Because it would be nice to your users if they didn't have to wait for the page to refresh before they found out they made a mistake.
    Also, how would I combine the code below
    Code:
    function validate() {
    
    if (document.email.value.length==0) {
    alert("please enter your email address");
    return false;
    }
    if (document.form.name.value.length==0) {
    alert("please enter your name");
    return false;
    }
    if (!document.form.name.value.match(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
    z0-9]|(-[A-Za-z0-9]))+)\.([A-Za-z]{2,6})(\.([A-Za-z]{2}))?$/) {
    alert("please enter a valid email address");
    return false;
    }
    
    document.form.submit()
    return true;
    }
    You should also not use JavaScript to submit the form.
    Code:
    <form action="process.php" method="post" onsubmit="return validate();">
    ...
    </form>

  10. #10
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's nigh on impossible to filter valid e-mails with regexp, especially now when non-ascii characters are allowed. At least &#229;&#228;&#246; and ' are valid in email addresses.

    If you don't want to filter out any valid email addresses by mistake then the safest bet it to just assume that there will be an @ somewhere in it, so
    Code:
    /^.+@.+$/
    Simon Pieters


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
  •