cancel a function

J

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>
 
M

McKirahan

Jon said:
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.
 
L

Lasse Reichstein Nielsen

Jon said:
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:
if(r == false) {

Pet peeve: don't compare to a boolean. Just write
if(!r) { ...


/L
 
J

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
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>
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top