Javascript help

TroganTrogan London, UK
edited April 2007 in Internet & Media
Hey all,

Following on from my Dreamweaver thread, I need some help with Javascript. I'm new to web development and programming, just so you know.

I've created an Email form, and this is the code I have for it:
[PHP]<form id="form1" name="form1" method="post" action="">
<p>Email
<label>
<input type="text" name="textfield" />
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
</p>
</form>[/PHP]
I need a piece of Javascript that will prompt a message if a valid/invalid Email is provided. With some help, this is what I have so far, but it doesn't seem to work correctly.
[PHP]function validate(){
at=document.form1.email.value.indexOf("@)
if (at==-1){
alert ("Not a valid email")
return false }
else
{alert ("This is a valid email")
return true
}[/PHP]

Hope someone can help. Thanks! :)

Comments

  • ShortyShorty Manchester, UK Icrontian
    edited April 2007
    [php]document.form1.email.value[/php]

    Should read:

    [php]document.form1.textfield.value[/php]

    For javascript to find the element in the document, it recurses down.

    Document (page)
    Form1 (your form)
    Element (in this case.. the input type of text, which you named textfield)
    Value of element (in this case the email address when they submit)

    Try that, that should work :)

    Also.. I don't see anything in the form with an "onSubmit" action. That will tell the form to invoke the javascript!

    Form declaration should read something like:
    [php]<form id="form1" onsubmit="validate();" name="form1" method="post" action="">[/php]
  • TroganTrogan London, UK
    edited April 2007
    Hi Dan - thanks for the advice and help.

    I receive this error message after entering an email address - valid or invalid.

    attachment.php?attachmentid=23075&stc=1&d=1177535131

    This is the code in Dreamweaver. Made the change regarding "textfield". Should I change "document" to "Homepage" since that is the name of the page?

    attachment.php?attachmentid=23074&stc=1&d=1177534953

    I've also included the "onSubmit" action:
    [PHP]<form id="form1" onsubmit="validate();" name="form1" method="post" action="">
    <p>Email
    <label>
    <input type="text" name="textfield" />
    </label>
    </p>
    <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
    </p>
    </form>[/PHP]

    Thanks again! :)
  • ShortyShorty Manchester, UK Icrontian
    edited April 2007
    Oops.. missed that bit in your code..

    Your jscript declaration should have a ; added to it:

    [php]
    function validate(){
    at=document.form1.email.value.indexOf("@)
    if (at==-1){
    alert ("Not a valid email")
    return false }
    else
    {alert ("This is a valid email")
    return true
    };
    [/php]

    Notice it is right at the end of the function?

    No need to change the page name. Document is the page..

    :)
  • TroganTrogan London, UK
    edited April 2007
    Dan, adding ';' didn't work, however adding '}' did.

    Thanks for your help - could not have done it without it. :)
  • ShortyShorty Manchester, UK Icrontian
    edited April 2007
    My bleary eyes @ 7am this morning didn't help that ;D

    Still, it's working now. Good work mate! :D
Sign In or Register to comment.