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

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6

    Can someone please explain this js code


    Hello all,

    I'm new and still learning. while i understand most of this script, im wondering if you good folks could please fill in the blanks.

    Code:
    function validateForm()
    {
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      {
      alert("Not a valid e-mail address");
      return false;
      }
    }
    the bit im kind of struggling with is the 'if' line:

    I understand that the code is evaluating an HTML form, and checking whether its been filled in correctly, im not sure that i am reading it right.

    I understand, i think, that:
    1. The atpos cannot be the first in the email address because of the atpos<1,
    2. The dotpos cannot be before the atpos owing to dotpos<atpos, but what does the +2 mean?
    Also,
    3. what is the +2 do here: dotpos+2>=x.length ...?

    apologies if im being dim.

    Kind regards
    MG
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,165
    Rep Power
    184
    It looks like it is just looking to see if the "@" symbol is in the email and if it contains a "." in the email address.

    Example => someone[@]somewhere[.]com

    The logic is to see if the text in the input field is formatted as an email address.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    atpos < 1
    requires there to be at least one character before the @ symbol in the email address. If atpos is 0 it means the string starts with @, if atpos is -1 it means the string does not contain @.

    +2 adds 2

    dotpos < atpos+2
    requires there to be at least one character between the @ sign and the dot. If dotpos and atpos were equal, it would mean that the same character is both an @ sign and a dot (which is not actually possible). If dotpos were less than atpos, it would mean the dot appears before the @ sign. If dotpos were equal to atpos+1 it would mean the dot immediately follows the @ sign.

    dotpos+2 >= x.length
    by similar logic, this requires there to be at least one character after the dot
    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
  6. #4
  7. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    Originally Posted by E-Oreo
    dotpos+2 >= x.length
    by similar logic, this requires there to be at least one character after the dot
    It means that the dot must be followed by at least two characters. (There are no top level domains with less than two characters.) More literally it means there must be two characters between the dot and the end of the string.

    Comments on this post

    • E-Oreo agrees : Had > stuck in my mind for some reason
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    How come i cant post a new thread?


    sry new to forums
    how come i cant create new threads i need help wit javascript
  10. #6
  11. 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 itsjonlin
    sry new to forums
    how come i cant create new threads i need help wit javascript
    Welcome to DevShed Forums, itsjonlin.

    Did you read the HOW TO POST A QUESTION - READ New Users thread?

    Also did your question include any URLs? New users are restricted from posting URLs until they have made 5 posts. You may need to get around this by leaving out the "http://" and putting a space before each ".". Yes this rule is annoying, but the administrators say it's necessary for limiting spam.
    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).
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    Hello All,

    As always, you good folks have come through once again - I really appreciate it.

    Thank you and kind regards
    MG
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    thank you kravvitz


    i appreciate it

IMN logo majestic logo threadwatch logo seochat tools logo