How does this form validation work?

In the javascript below, I understand what line 8 means, but I don't understand part of lines 9 and 10.

To me, line 9, in human language, means "if the index value of the last dot is less than 2 greater than the @'s index value." Is that correct?

And line 10, to me, means "if the index value of the last dot is within the last two spots of the array." Is that correct?

<script type="text/javascript">
function validateForm()
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (
    atpos<1 || /*the @ cannot be the first character*/
    dotpos<atpos+2 || 
  alert("Not a valid e-mail address");
  return false;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

So, x is the string value representing the contents of the email field on the form.

And atpos and dotpos are index values returned for operations done on the string x, finding the location of the (first) @ character and the last . character, respectively.

The if statement is checking for at least one of the conditions (found on lines 8, 9, and 10) to be true.

Line 8 checks we're not getting @ in the first spot in the email address.

Line 9 looks like it is meant to check for times when the email address entered has its last dot before the at sign.  (Note that it does not handle the case where there is no dot present.)  For example, email.address@domain would be "true" for this check and would set off the alert message.

The reason for the +2 part appears to be trying to check for the substring between the @ and the . is at least 2 characters long.  If that is less than 2, this condition will also be "true" for this rule.  For example, would throw the alert.

Line 10 is intended as a check to make sure that there are at least 2 characters in the email address after the last dot.  For example, email@domain.t would be "true" for this check and set off the alert.

I hope this is helpful.  Please let me know if I've been unclear or missed something.

A side note:  this is not an example of a robust email address validator.
Line 9 says that last dot shouldn't be there as immediate next character of @
Line 10 says that there should be at least one character after last dot

Above conditions are for checking email format
@olivepeople : There is correction in your comment.

Line 9 says that there should be atleast one character between last 'dot' and first @
Line 10 says that there should be atleast one  character after last 'dot'
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

shampouyaAuthor Commented:
If line 9 says that there should be at least 1 character between @ and 'dot', I think it should be written like this: atpos >= dotpos +1

If line 10 says that there should be at least 2 characters after 'dot', I think it should be written like this: dotpos + 2 < x.length

Explain to me why I'm wrong
@shampouya :

Actual Code:


dotpos<atpos+2 ==> (There should be no character gap between 'dot' and @) or ('dot' should come before @)


dotpos+2>=x.length ==> There can be only one or no character after 'dot'

You code in previous post:


atpos >= dotpos +1 ==> @ should be there atleast one character after 'dot'


dotpos + 2 < x.length ==> There should be minimum of 2 characters after dot
shampouyaAuthor Commented:
Ok let me rephrase what I think these three statements mean now. Am I correct?

atpos<1 ||     /*if @ comes before the second array element*/
dotpos<atpos+2 ||  /*if 'last dot' is only one spot after @, or less*/
dotpos+2>=x.length)  /*if 'last dot' is in the last or 2nd-to-last spot of the array*/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shampouyaAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.