November 13th, 2012, 10:52 AM
Can someone please explain this js code
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.
the bit im kind of struggling with is the 'if' line:
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
alert("Not a valid e-mail address");
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?
3. what is the +2 do here: dotpos+2>=x.length ...?
apologies if im being dim.
November 13th, 2012, 08:06 PM
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.
November 13th, 2012, 08:11 PM
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
November 13th, 2012, 08:27 PM
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.
Originally Posted by E-Oreo
Comments on this post
November 13th, 2012, 08:35 PM
How come i cant post a new thread?
sry new to forums
November 13th, 2012, 09:11 PM
November 14th, 2012, 05:26 AM
As always, you good folks have come through once again - I really appreciate it.
Thank you and kind regards
November 15th, 2012, 03:04 PM