JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 13th, 2012, 10:52 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Other - 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

Reply With Quote
  #2  
Old November 13th, 2012, 08:06 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 16 h 17 m 48 sec
Reputation Power: 69
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.

Reply With Quote
  #3  
Old November 13th, 2012, 08:11 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,939 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 9 h 12 m 42 sec
Reputation Power: 7053
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
How to program a basic, secure login system using PHP

Quote:
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

Reply With Quote
  #4  
Old November 13th, 2012, 08:27 PM
Nilpo's Avatar
Nilpo Nilpo is offline
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: New Springfield, OH
Posts: 863 Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)Nilpo User rank is General 3rd Grade (Above 100000 Reputation Level)  Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2Folding Points: 600856 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 5 Days 16 h 50 m 17 sec
Reputation Power: 1185
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Quote:
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 &gt; 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!

Reply With Quote
  #5  
Old November 13th, 2012, 08:35 PM
itsjonlin itsjonlin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 itsjonlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 m 22 sec
Reputation 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

Reply With Quote
  #6  
Old November 13th, 2012, 09:11 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,834 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 21 h 3 m 17 sec
Reputation Power: 4192
Quote:
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. I'm available for hire at Dynamic Site Solutions.

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).

Reply With Quote
  #7  
Old November 14th, 2012, 05:26 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Hello All,

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

Thank you and kind regards
MG

Reply With Quote
  #8  
Old November 15th, 2012, 03:04 PM
itsjonlin itsjonlin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 itsjonlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 m 22 sec
Reputation Power: 0
thank you kravvitz

i appreciate it

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Other - Can someone please explain this js code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap