how to prevent a form to be submiited?

B

ben

Hi,

I made a form for sending mails to me. But before the mail is sent, the name
must be filled, and as long it's not done, the same message must appear. I
tried with different way (submit inputbox, button inputbox, action in
jscript, action in the form definition line ...)
See the code:

<script language=javascript>

function stp()

{

if (document.getElementById("in1").value.length == 0)

{

alert("you forgot your name");

}


}

</script>

<FORM id=f1 action="http://myprovider/cgi-bin/FormMail.pl" METHOD="POST">

input type=hidden name="env_report" value="REMOTE_ADDR,HTTP_USER_AGENT">

input type=hidden name="recipient" value="(e-mail address removed)">

Put your name: <input id=in1 type="text" size="40" name="na">

input type="submit" name="knp" value="send" onclick="stp()">

</form>
 
M

Martin Honnen

ben said:
Hi,

I made a form for sending mails to me. But before the mail is sent, the name
must be filled, and as long it's not done, the same message must appear. I
tried with different way (submit inputbox, button inputbox, action in
jscript, action in the form definition line ...)
See the code:

<script language=javascript>

function stp()

{

if (document.getElementById("in1").value.length == 0)

{

alert("you forgot your name");

}


}

function validateForm (form) {
if (form.in1.value == "") {
alert('Fill in your name.');
form.in1.focus();
form.in1.select();
return false;
}
else {
return true;
}
}
</script>

<FORM id=f1 action="http://myprovider/cgi-bin/FormMail.pl" METHOD="POST">

<form onsubmit="return validateForm(this);"
input type=hidden name="env_report" value="REMOTE_ADDR,HTTP_USER_AGENT">

input type=hidden name="recipient" value="(e-mail address removed)">

Put your name: <input id=in1 type="text" size="40" name="na">

<input type="text" name="in1"
 
D

Daniel

ben said:
function stp()
{
if (document.getElementById("in1").value.length == 0)
{
alert("you forgot your name");
}
}
<FORM id=f1 action="http://myprovider/cgi-bin/FormMail.pl" METHOD="POST">
input type=hidden name="env_report" value="REMOTE_ADDR,HTTP_USER_AGENT">
input type=hidden name="recipient" value="(e-mail address removed)">
Put your name: <input id=in1 type="text" size="40" name="na">
input type="submit" name="knp" value="send" onclick="stp()">
</form>

First, give the form a name, e.g. name="formName".

Try, instead of a "submit" input, a "button" input.

Then, in stp(), if all is well do a...

document.formName.submit();

Hope this helps =)

Daniel
 
J

Jeff Cochran

Hi,

I made a form for sending mails to me. But before the mail is sent, the name
must be filled, and as long it's not done, the same message must appear.

There are a number of ways to do form validation in Javascript, and a
Google of "Javascript Form Validation" will get you plenty of
examples. For instance:

http://www.javascripter.net/faq/validati.htm
http://www.echoecho.com/jsforms.htm
http://developer.netscape.com/docs/examples/javascript/regexp/overview.html

And probably a bazillion others.

Jeff
===================================
Jeff Cochran (IIS MVP)
(e-mail address removed) - Munged of Course

I don't get much time to respond to direct email,
so posts here will have a better chance of getting
an answer. Besides, everyone benefits here.

Suggested resources:
http://www.iisfaq.com/
http://www.iisanswers.com/
http://www.iistoolshed.com/
http://securityadmin.info/
http://www.aspfaq.com/
http://support.microsoft.com/
====================================
 
S

Stuart Palmer

I'd go with putting the validation onsubmit in the form tag:-

<script language=javascript>
function Validate()
{
if (document.getElementById("in1").value.length == 0)
{
alert("you forgot your name");
return false;
}
else
{
return true;
}
}
</script>

<FORM id=f1 action="http://myprovider/cgi-bin/FormMail.pl" METHOD="POST"
onsubmit="return(Validate());">
<input type=hidden name="env_report" value="REMOTE_ADDR,HTTP_USER_AGENT">
<input type=hidden name="recipient" value="(e-mail address removed)">
Put your name: <input id=in1 type="text" size="40" name="na">
<input type="submit" name="knp" value="send">
</form>

Personally I am not a fan of putting javascript functions in buttons. but
that is personal preference.

Stu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top