Forum Moderators: open

Message Too Old, No Replies

Form validator

Problems with the javascript, it is sending mail instead...

         

jendo

8:37 am on Feb 18, 2004 (gmt 0)

10+ Year Member



Hello,

i'm just another designer with no javascript skills who has a problem with a form validation javascript.

Thought it works well displaying a error, it sends the a mail anyway and continues to load the follow-up page. What do i need to change in the function to make the script say on the form-page?

<snip>

Javascript code:
function validate() {
if(document.form.name.value=='' ¦¦ document.form.firma.value=='' ¦¦ document.form.email.value==''){
alert('Bitte geben Sie Name, Firma und eine gültige E-MailAdresse an.')
} else {
document.form.submit()
}
}

Form code:

<FORM ACTION="http://www.21st-century.net/utilities/mailto.asp" METHOD=POST name=form>
<INPUT NAME="name" TYPE=text id="name" SIZE=30>
<INPUT NAME="firma" TYPE=text id="firma" SIZE=30>
<INPUT NAME="email" TYPE=text id="email" SIZE=30>
<INPUT type=image src="media/produkt/weiter.gif" width=57 height=18 border=0 align=bottom VALUE='' onClick='validate()'>

<INPUT TYPE=hidden NAME=subject VALUE="*** Test Bauguss Katalog ***">
<INPUT TYPE=hidden NAME=server VALUE="smtp.visualize.ch">
<INPUT TYPE=hidden NAME=sendto VALUE="jendo.neversil@visualize.ch">
<INPUT TYPE=hidden NAME=resulturl VALUE="http://www.nottaris.ch/download.html">
<INPUT TYPE=hidden NAME="" VALUE="">
</FORM>

Any hint highly appreciated.

Regards, Jendo

[edited by: korkus2000 at 4:09 pm (utc) on Feb. 18, 2004]
[edit reason] no code reviews please [/edit]

jendo

10:08 am on Feb 18, 2004 (gmt 0)

10+ Year Member



It works, but only with no image button.

Thanks to myself,

Regards, Jendo

dcrombie

2:37 pm on Feb 18, 2004 (gmt 0)



You should change the function to:

function validate(form) {
if (form.name.value == '' ¦¦ form.firma.value == '' ¦¦ form.email.value == ''){
alert ('Bitte geben Sie Name, Firma und eine gültige E-MailAdresse an.');
return false;
}
return true;
}

Then, remove the 'onClick' from your button and instead edit the FORM line:

<FORM ... onSubmit="return validate(this);">
...
</FORM>

Then you should have no problem using any type of button - even hitting Enter will trigger the validation script.