#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    21
    Rep Power
    0

    Email validation


    Hi,

    i have this code to check to make sure email is entered but don't know how to valid it, can you plz help?
    <script language="JavaScript">
    function validateForm(oForm) {

    if (document.getElementById ("email").value.length == 0) {
    alert ("Email must be entered.");
    return false;
    }
    return true;

    }
    </script>


    <form action="process.cfm" method="post" name="form" id="form" onsubmit="return validateForm(this);">
    Email<br /> <input type="text" name="email" id="email" size="24" /
    </form>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,158
    Rep Power
    183
    The code you have is only set-up to check the length of your email input field. It doesn't check to see if it actually contains characters; that would appear in an actual email address. You would need to create a Regular Expression to check for these characters. Take a look at this tutorial from JavaScript Kit About "Advanced Email Verification"; so you will get a basic idea how to do this.
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Originally Posted by web_loone08
    Take a look at this tutorial from JavaScript Kit About "Advanced Email Verification"; so you will get a basic idea how to do this.
    That will block valid addresses.

    Here's a much better option: http://stackoverflow.com/questions/4...-in-javascript
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,158
    Rep Power
    183
    Originally Posted by Kravvitz
    That will block valid addresses.
    What?
    Originally Posted by Description
    A script that closely examines the content of a form box to ensure that the user entered a valid email address. If not, the form submition is canceled, and the surfer prompted to re-enter a valid address. The script makes the following assumptions regarding what a valid email address is:

    -Contains a least one character procedding the "@"
    -Contains a "@" following the procedding character(s)
    -Contains at least one character following the "@", followed by a dot (.), followed by either a two character or three character string (a two character country code or the standard three character US code, such as com, edu etc)
    Test it out; it validates to see if email address is valid. It does not block valid email addresses; it accepts them and submits form. Otherwise..., if email address is invalid; you get the alert.

    That is a good post though; from stackoverflow, maybe somewhat advanced for OP's early JS conceptions, possibly.
    Last edited by web_loone08; June 17th, 2013 at 11:17 PM.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Test it out; it validates, not blocks.
    He means that it blocks some valid email addresses, not that it blocks all valid email addresses. For example: "john+doe@gmail.com"

    My advice for email validation: just check to see if the string contains at least one @ sign. The point of email syntax validation is to help the user from making a mistake like mixing the fields up and entering their phone number into the field instead of an email address. The point of email syntax validation is not to make them enter a valid email address if they don't want to do it, because you will never succeed at that with a regular expression.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,158
    Rep Power
    183
    Originally Posted by E-Oreo
    He means that it blocks some valid email addresses, not that it blocks all valid email addresses. For example: "john+doe@gmail.com"
    Oh, ok..., yeah, there are several more advanced versions out there; that try to encapsulate every possible dynamic character scheme, but there is usually a draw back (of some kind) to most all of them. Although, you should always back-up validation on the server-side; I am a pretty big fan of HTML5's built in validation (granted; if browser does not support HTML5, then you need a JS fallback or I guess you could just rely on server-side validation, as a fail-safe method).
    Last edited by web_loone08; June 17th, 2013 at 11:31 PM.
  12. #7
  13. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Originally Posted by web_loone08
    What? Test it out; it validates to see if email address is valid. It does not block valid email addresses; it accepts them and submits form. Otherwise..., if email address is invalid; you get the alert.
    By "block" I mean it will not allow the form to be submitted when certain valid addresses are used (including ones that I use -- or at least attempt to use -- on a regular basis). More info:
    http://isemail.info/about
    http://en.wikipedia.org/wiki/Email_address#Address_tags

    Since the prose does not fully describe the regex used in the script, it's rather irrelevant.

    Originally Posted by web_loone08
    That is a good post though; from stackoverflow, maybe somewhat advanced for OP's early JS conceptions, possibly.
    Unfortunately, writing good form validation requires a certain skill level.

    Comments on this post

    • web_loone08 agrees : Yeah, I get what you meant now and yes..., it does.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  14. #8
  15. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Originally Posted by web_loone08
    Oh, ok..., yeah, there are several more advanced versions out there; that try to encapsulate every possible dynamic character scheme, but there is usually a draw back (of some kind) to most all of them.
    Some are written to allow any address that is technically valid, however, my point is to not block commonly used address, like the example given by E-Oreo, which is referred to as "plus addressing" by the way. Plus addressing is very useful in conjunction with Gmail's filters. However, many sites block it either through ignorance or due to a few idiot spammers. Though why they can't just do a replace of "\+[^@]+@" with "@" before doing their checks for validity, uniqueness (in their database of users), etc. to avoid the problem, I don't know.

    @E-Oreo, do you use plus addressing?

    Originally Posted by web_loone08
    Although, you should always back-up validation on the server-side; I am a pretty big fan of HTML5's built in validation (granted; if browser does not support HTML5, then you need a JS fallback).
    Yeah, the HTML5 form validation is great. I don't consider a JS fallback strictly necessary, but that should be decided on a case-by-case basis.

    Originally Posted by E-Oreo
    My advice for email validation: just check to see if the string contains at least one @ sign. [...] The point of email syntax validation is not to make them enter a valid email address if they don't want to do it, because you will never succeed at that with a regular expression.
    Site owners seem to feel better if users are forced to provide something that's at least syntactically valid.
    Last edited by Kravvitz; June 17th, 2013 at 11:44 PM.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  16. #9
  17. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I use it extensively when testing registration systems that require unique email addresses. I rarely use it for filtering, but if I'm programming a script that automatically emails me I'll normally add one.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo