Solved

How does this form validation work?

Posted on 2012-03-10
8
205 Views
Last Modified: 2012-03-14
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 || 
    dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>

Open in new window

0
Comment
Question by:shampouya
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:olivepeople
ID: 37705979
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.
0
 
LVL 1

Expert Comment

by:ravi_kaalla
ID: 37706171
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
0
 
LVL 1

Expert Comment

by:ravi_kaalla
ID: 37706175
@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'
0
 

Author Comment

by:shampouya
ID: 37706192
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
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:ravi_kaalla
ID: 37706911
@shampouya :

Actual Code:

1.

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

2.

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

You code in previous post:

1.

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

2.

dotpos + 2 < x.length ==> There should be minimum of 2 characters after dot
0
 

Author Comment

by:shampouya
ID: 37718483
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*/
0
 
LVL 1

Accepted Solution

by:
ravi_kaalla earned 500 total points
ID: 37719157
Yes
0
 

Author Closing Comment

by:shampouya
ID: 37721551
thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now