By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

how to prevent a form to be submiited?

P: n/a
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="my*****@zerz.vb">

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

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

</form>
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


ben wrote:
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="my*****@zerz.vb">

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

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

</form>

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a

"ben" <df****@sdfs.fb> wrote in message news:bd**********@reader11.wxs.nl...
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="my*****@zerz.vb">
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

--
There are 10 kinds of people: Those who know binary and those who don't.


Jul 20 '05 #3

P: n/a
On Wed, 2 Jul 2003 15:37:00 +0200, "ben" <df****@sdfs.fb> wrote:
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/e.../overview.html

And probably a bazillion others.

Jeff
===================================
Jeff Cochran (IIS MVP)
jc*************@naplesgov.com - 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/
====================================
Jul 20 '05 #4

P: n/a
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="my*****@zerz.vb">
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

"ben" <df****@sdfs.fb> wrote in message news:bd**********@reader11.wxs.nl...
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="my*****@zerz.vb">

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

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

</form>

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.