Jump to content


need some help with a javascript email validator


14 replies to this topic

#1 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 13 July 2005 - 09:26 AM

i'm trying to make an email validator that if the email is valid then javascript or HTML sends it to the email address the user submitted, i'd like to do most of this if not all of it by myself, so, dont give me the entire code or anything, i'd like to know what statements i should use (ex. if, else, contains, defaultvalue) <<those are the ones i think i should use... if you see that i'm missing one let me know, i need a validator code, i'm not sure on how to go about the validation and error returning, i also need to know how to send the email (predetermined message) to the specified email address AND the person's name who sent the email

here's the setup if you dont have a clue what i'm talking about
                             Tell friend's about our site
Friend's email address-|__________|   Your name-|_________|

                                          ***Key*** 
|________| = text boxes that are single lined (<input type="text"...)

how the validator will validate the "Freind's Email address"
in ONLY that field javascript will look for the following sign @ if the field has that then javascript looks for an extension (ex. .net, .org, .com, .edu) then if it has ONE of those extensions then it checks to see were in the field the extension falls, obviously i want javascript to check if the extensions are the LAST 4 characters in the email field then if this all checks out then the email is sent... i dont know the code to do this with javascript

if anyone could help i'd appreciate it!

Edited by Hacker-X, 13 July 2005 - 09:27 AM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 July 2005 - 12:57 PM

http://www.pixel2life.com/tutorials/JavaSc...=1&d=1&ss=email

One of those should answer your question.

#3 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 13 July 2005 - 08:40 PM

lol, i didn't want the code but thank you very much, i have a code i made and i'm going to test it, but if it doesn't work out, i'll look at those tutorials, thanks

EDIT: i made a code, believe it or not, lol, from references from a javascript book i got, anyways i've come across an error, i used the javascript console on mozilla firefox and it says (when i run the script) "missing ) after condition", the line it finds the error is this: return (true) it works almost the same way as the validator does

here's the code i'm using...
<script LANGUAGE="JavaScript">
function isblank(name) {
if (name.length=0(emailform.name.value)
return (true)
}
alert("You must include your name!")
return (false)
}

function checkEmail(emailform) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform.Email.value)){
return (true)
}
alert("Invalid E-mail Address!")
return (false)
}
</script>

<form onSubmit="return checkEmail(this)">
E-mail Address:<br>
<input type="text" name="Email">
<br>
<form onSubmit="return isblank(this)">
Your name:<br>
<input type="text" name=name>
<input type="submit" value="Send">
</form>

help and explaination is appreciated

ps. what does this line mean?
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform.Email.value)){

i know the if and the (emailform.Email.value)){ part but i dont understand the
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test symbols i know the code is used to decide if the email is validated or invalid but whats the *,[,],w,+ and all those characters do (the book i have doesn't go into detail about the characters, and i've haven't read through the entire book yet)

Edited by Hacker-X, 13 July 2005 - 10:37 PM.


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 July 2005 - 10:47 PM

First off... what the heck is this!?
function isblank(name) {
if (name.length=0(emailform.name.value) // more specifically, this line
return (true)
}
alert("You must include your name!")
return (false)
}

Second, that line is simply a regular expression. It sets a pattern for what characters you can use, in what order, how many, etc...

p.s. After you figure out what that first function is, you may want to change the rest of your code to this:
function checkEmail(emailform) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform.Email.value)){
return (true);
}
alert("Invalid E-mail Address!");
return (false);
}
I simply added semi-colons, they may not be neccessary, but i do believe you're "supposed" to have them there.

#5 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 13 July 2005 - 11:10 PM

rc69, on Jul 14 2005, 03:47 AM, said:

First off... what the heck is this!?
function isblank(name) {
if (name.length=0(emailform.name.value) // more specifically, this line
return (true)
}
alert("You must include your name!")
return (false)
}

Second, that line is simply a regular expression. It sets a pattern for what characters you can use, in what order, how many, etc...

p.s. After you figure out what that first function is, you may want to change the rest of your code to this:
function checkEmail(emailform) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform.Email.value)){
return (true);
}
alert("Invalid E-mail Address!");
return (false);
}
I simply added semi-colons, they may not be neccessary, but i do believe you're "supposed" to have them there.
i'm a noob :ph34r: , is that a good excuse? lol, what should the isblank function code be? i'm guessing i got the s.length thing wrong too? i thought if i put that jscript would check if the field was empty and if it was empty then it would have an alert once the user clicks send

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 July 2005 - 11:30 PM

well, it's an iffy excuse... i just couldn't make heads or tails of that line. If you want it to check if the field is blank try this:
function isblank(name){
    if(name.length < 1){
        alert("Please enter a name.");
        return false;
    }

    return true;
}

That should make a little more sense to you and the browser (also it may just get rid of that error for you).

#7 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 14 July 2005 - 09:47 AM

thanks, thats what i mean't, :D

lol, the one code for the name function doesn't work, it doesn't have any errors but it just doesn't work

here's the code i'm using...
<script LANGUAGE="JavaScript">
function isblank(name){
   if(name.length < 1){
       alert("Please enter a name.");
       return false;
   }
   return true;
}

function checkEmail(emailform) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform.Email.value)){
return (true);
}
alert("Invalid E-mail Address!");
return (false);
}
</script>

<form onSubmit="return checkEmail(this)">
E-mail Address:<br>
<input type="text" name="Email">
<br>
<form onSubmit="return isblank(this)">
Your name:<br>
<input type="text" name=name>
<input type="submit" value="Send">
</form>

*i have a feeling i'm missing something after the first fuction

Edited by Hacker-X, 14 July 2005 - 09:53 AM.


#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 July 2005 - 01:28 PM

half the reason could be, because you have 2 forms in one, and one closing tag for both... which isn't to smart.
try this for your form:
<form onSubmit="return checkFrom(this);">
E-mail Address:<br>
<input type="text" name="email">
<br>
Your name:<br>
<input type="text" name="name"><br>
<input type="submit" value="Send">
</form>
And then change your javascript to this:
<script language="JavaScript">
function checkForm(form){ // New function
    if(form.name.length < 1){ // Checks name
        alert("Please enter a name.");
        return false;
    }

    if(checkEmail(form) === false){ // Kept the checkEmail function
        return false;
    }

    return true;
}

function checkEmail(emailform) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform.email.value)){
    return true;
    }
    alert("Invalid E-mail Address!");
    return false;
}
</script>

Hopefully that should work out better for you (you can remove the comments i added). You might also want to add a method and action attribute to your form.

#9 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 15 July 2005 - 08:53 AM

lol, it still doesn't work, it doesn't return any errors, if you leave the name field blank OR when you type in a fake email address ex. */-/***/-, thanks for trying to help me though

EDIT: found the reason why the name validator didn't work the code was
if(form.name.length == <1) i changed this to: if(form.name.value) so now it works, thanks for the code and comments within the code... now i get to laugh in an experienced javascripters face :angry:, joking

now i need to (if everything is correct) send out the email to the user's email address probably using this code to get the email address
some keyword(form.email.value)
and then use the name of the person who is sending the email (in the email who the recipient is getting) using this code:
some keyword(form.name.value)
and last but not least include a message (that is "static", which means it never changes)

so here's the run down

i type my friend's email into the email text line
then i type my name into the text line

then if everything is correct on the form then it is sent to my FRIEND's email address and it'll have MY name on the email with a message that is static

does sound too hard, does it, :)

Edited by Hacker-X, 15 July 2005 - 09:23 AM.


#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 July 2005 - 11:42 AM

Laugh in my face all you want, i'm a PHP programmer, not javascript.

But one thing i do know is, you cannot send e-mail through javascript, you need php (or some other server side script) for that.

#11 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 15 July 2005 - 09:58 PM

ok, so your saying out of more than 2,000 functions, there isn't ONE to send an email? lol, alright thanks anyways, but can't i use an HTML script (mailto:...) to send it to the person's email? i've seen email's being sent with javascript and html...

Edited by Hacker-X, 15 July 2005 - 10:00 PM.


#12 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 July 2005 - 11:02 PM

actually, now that you mention it, i do believe that you can send an e-mail using action="mailto:name@domain.com"
Not 100% though. Also, just for you, you may want to add this to your function:
form.action='mailto:'+form.email;

Put that IN the actuall function, and it may work, not sure though.

#13 Wybe

    Jedi In Training

  • Members
  • PipPip
  • 399 posts
  • Gender:Male
  • Location:the Netherlands
  • Interests:Graphic design, digital and traditional, street style, graffiti, guerilla drawing, typography, coding, sex

Posted 22 July 2005 - 04:13 PM

what if someone has javascript disabled?

#14 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 31 July 2005 - 06:07 PM

Wybe, on Jul 22 2005, 09:13 PM, said:

what if someone has javascript disabled?
then the code wouldn't work

#15 adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 01 August 2005 - 03:41 AM

rc69, on Jul 16 2005, 05:02 AM, said:

actually, now that you mention it, i do believe that you can send an e-mail using action="mailto:name@domain.com"
Doesn't that just open your default email program?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users