We help IT Professionals succeed at work.

How does this form validation work?

shampouya asked
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

Watch Question

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, email@m.com 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'


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


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*/



Explore More ContentExplore courses, solutions, and other research materials related to this topic.