Closed Thread Icon

Topic awaiting preservation: Validating Email Address (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8773" title="Pages that link to Topic awaiting preservation: Validating Email Address (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Validating Email Address <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 07-24-2003 00:14

Hi, guys. I am doing reverse-enginering of this validating email address script.
There are couple of points I cannot understand. Would you mind telling me what they mean, please???

Here is my code:

code:
<html>
<head>
<script type="text/javascript">
<!--
function validEmail(email){

invalidChars = " /:,;";

if(email == ""){

return false;

}/*End of if*/

for(i=0; i<invalidChars.length; i++){

badChar = invalidChars.charAt(i);
if(email.indexOf(badChar, 0)>-1){

return false;

}/*End of If*/

}/*End of loop*/

atPos = email.indexOf("@", 1);

if(atPos == -1){

return false;

}/*End of if*/

if(email.indexOf("@", atPos+1) >-1){

return false;

}/*Enf of if*/

periodPos = email.indexOf(".", atPos);

if(periodPos == -1){

return false;

}/*End of If*/

if(periodPos+3>email.length){

return false;

}/*End of if*/

return true;

} /*End of function validEmail*/


function submitIt(carForm){

if(!validEmail(carForm.emailAddr.value)){

alert("Invalid email address");
carForm.emailAddr.focus();
carForm.emailAddr.select();
return false;

}/*End of if*/

return true;

}/*end of function*/
-->
</script>
</head>
<body>
<form onSubmit="return submitIt(this)" name="myForm">
<h3>Car Picker</h3>
<p>Email address: <input type="text" name="emailAddr"></p>
<p><input type="submit" value="submit">  <input type="reset" value="reset"></p>
</form>
</body>
</html>



What I don't understand is:

code:
for(i=0; i<invalidChars.length; i++){

badChar = invalidChars.charAt(i);
if(email.indexOf(badChar, 0)>-1){

return false;

}



What are they??? Especially those two lines:

badChar = invalidChars.charAt(i);
if(email.indexOf(badChar, 0)>-1)

And this part:

code:
atPos = email.indexOf("@", 1);

if(atPos == -1){

return false;

}/*End of if*/



I saw a lot of points including something like:

email.indexOf("@", 1);

Actually what does it do???

Well, thanks for reading this.
Looking forward to hearig from you!

Ps. Here is my upload file: Click!
Hiroki Kozai

[This message has been edited by Hiroki (edited 07-24-2003).]

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-24-2003 03:37

look at this line-

invalidChars = " /:,;";

those are the bad chars and that for loop cycles thru each one looking for it in the email addy..

that other part makes sure that there is at least one character after the '@' in the email address..
...it get the character after the @ and if it's null (-1) then it returns 'false'..



Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-24-2003 03:47

badChar = invalidChars.charAt(i);

This takes the i'th character of invalidChars, and assigns it (as a string with length 1) to badChar. For instance, since invalidChars is " /:,;" (it's assigned that value at the top of the script), then when i=0, badChar will be " ", when i=1, badChar will be "/", when i=2, badChar will be ":", etc. The loop goes through every character in invalidChars this way. The purpose of this is to make sure that none of the characters in invalidChars is in the email address. That's what the next line does:

if(email.indexOf(badChar, 0)>-1)

This line can be read as "if (email contains badChar)." indexOf is a way of checking if one string contains another string. Take this example:

A.indexOf(B)

if A contains B, then this will return the position at which B starts within A. (0 is the first position, 1 is the second, etc.) For instance, ("hello").indexOf("llo") returns 2. ("hello").indexOf("he") returns 0. If B is not found in A, then this returns -1.

The second argument of indexOf is optional, and specifies that you should start searching at the given location (ignoring B if it starts before that location). When this is zero, it has no effect, so I don't know why the writer bothered to specify it.

So, that line checks if the badChar is in the email. If it is, then email.indexOf(badChar, 0) will be zero or above, which is greater than -1, so the function will return false.

By looping through every character in invalidChars, it makes sure that the email doens't contain any invalid characters. (A safer and more thorough way to do this would be for it to contain a list of characters that *are* valid, and if the email contains anything *else*, returning false. You might want to try to figure out how to do that for practice.)

email.indexOf("@", 1);

As I explained above, this line will return the first position of the "@" character within the string email. If the "@" character is not found, this will return -1. Note that since the second argument is 1, and not 0, this will return -1 even if the "@" character is the very first character in email. It must be the second character or later within email.

Later on, the script uses the line

periodPos = email.indexOf(".", atPos);

using atPos as the second argument ensures that the period is only found if it's after the "@". If the only period is *before* the "@", periodPos will be -1.

trib
Paranoid (IV) Inmate

From: Den Haag, Netherlands
Insane since: Sep 2002

posted posted 07-24-2003 15:11

Seems a bit long-winded when you can do it with a regexp (for browsers > IE4/NS4) ... see http://www.juicystudio.com/tutorial/javascript/regexp.html for an example.

This ought to work ... <BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>function validEmail(eMailAdd) {
var rex1 = new RegExp("(@.*@)

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 07-25-2003 00:57

Hi, guys.
Many many thanks for your helps.
I am really glad to hear from you, guys.
How are you today? We've got heavy frost this morning. the road gets very slipperly.
It was a bit hard to walk.
Slime:
I printed out your comment. I read it carefully last night after dinner.
It was great explanation. I got a lot of points of what I wanted to know.

1. badChar = invalidChars.charAt(i);

This takes the i'th character of invalidChars then assing to badChar

2. A.indexOf(B)

if A contains B

3. email.indexOf("@", 1);

If email cotrains @ at the second plance of itself

Now I am making sure I understand rest of all the code.
Many many thanks to you.
See you later.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu