Javascript help
Trogan
London, UK
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!
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!
0
Comments
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]
I receive this error message after entering an email address - valid or invalid.
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?
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!
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..
Thanks for your help - could not have done it without it.
Still, it's working now. Good work mate!