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

cancel a function

P: n/a
Jon
Hi, I have a form. I have the code, below, to check for certain compulasry
fields. The Submit button has:

onClick="validate(this.form)"

My problem is that the warnings come up fine, but the form still gets
posted. Please can someone advise how I cancel the function if data is
missing?

Thank. Jon

<SCRIPT language="JavaScript" type="text/javascript">
<!--
function validate(x) {
var e = "";
var r = true;

if(x.txtName.value == "") {
e += "\n\nPlease enter a name";
r = false;
}

if(x.txtCompany.value == "") {
e += "\n\nPlease enter a company name";
r = false;
}
if(x.txtPhone.value == "") {
e += "\n\nPlease enter a phone number";
r = false;
}
if(x.txtEmail.value == "") {
e += "\n\nPlease enter an email address";
r = false;
}
if(r == false) {
alert("The following must be completed first:"+e);
}
return r;
}
// -->
</SCRIPT>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Jon" <jon@SPAM_OFFtheexperts.co.uk> writes:
Hi, I have a form. I have the code, below, to check for certain compulasry
fields. The Submit button has:

onClick="validate(this.form)"
You mean
onclick="return validate(this.form);"

I prefer to put it on the form tag:
<form ... onsubmit="return validate(this);">
if(r == false) {


Pet peeve: don't compare to a boolean. Just write
if(!r) { ...
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
"Jon" <jon@SPAM_OFFtheexperts.co.uk> wrote in message
news:br**********@hercules.btinternet.com...
Hi, I have a form. I have the code, below, to check for certain compulasry fields. The Submit button has:

onClick="validate(this.form)"

My problem is that the warnings come up fine, but the form still gets
posted. Please can someone advise how I cancel the function if data is
missing?

Thank. Jon

<SCRIPT language="JavaScript" type="text/javascript">
<!--
function validate(x) {
var e = "";
var r = true;

if(x.txtName.value == "") {
e += "\n\nPlease enter a name";
r = false;
}

if(x.txtCompany.value == "") {
e += "\n\nPlease enter a company name";
r = false;
}
if(x.txtPhone.value == "") {
e += "\n\nPlease enter a phone number";
r = false;
}
if(x.txtEmail.value == "") {
e += "\n\nPlease enter an email address";
r = false;
}
if(r == false) {
alert("The following must be completed first:"+e);
}
return r;
}
// -->
</SCRIPT>

Change it to the following; watch for word-wrap:

Add " onSubmit="return validate(this.form)" to your "form" tag
and remove the "onClick=" from the Submit button.
Jul 20 '05 #3

P: n/a
Jon
Don't worry - sorted it.
The button was still a submit button, and I've added in a form submit at the
end of this script

Jon
"Jon" <jon@SPAM_OFFtheexperts.co.uk> wrote in message
news:br**********@hercules.btinternet.com...
Hi, I have a form. I have the code, below, to check for certain compulasry
fields. The Submit button has:

onClick="validate(this.form)"

My problem is that the warnings come up fine, but the form still gets
posted. Please can someone advise how I cancel the function if data is
missing?

Thank. Jon

<SCRIPT language="JavaScript" type="text/javascript">
<!--
function validate(x) {
var e = "";
var r = true;

if(x.txtName.value == "") {
e += "\n\nPlease enter a name";
r = false;
}

if(x.txtCompany.value == "") {
e += "\n\nPlease enter a company name";
r = false;
}
if(x.txtPhone.value == "") {
e += "\n\nPlease enter a phone number";
r = false;
}
if(x.txtEmail.value == "") {
e += "\n\nPlease enter an email address";
r = false;
}
if(r == false) {
alert("The following must be completed first:"+e);
}
return r;
}
// -->
</SCRIPT>

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.