473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

cancel a function

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
3 2155
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven | last post by:
Hello I need some help with canceling a thread. Here is a simple code that creates and cancels a thread. It does create a thread BUT it doesnt cancel it, am I doing this wrong? Thank you! ...
11
by: ryanmhuc | last post by:
I have a function which needs to cancel the input into a text field on a form. I cancel the event in IE fine and the text does not get entered. But for firefox (1.0) I cancel the event and the text...
6
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
5
by: lsolesen | last post by:
I used the following javascript to add confirm-boxes to links. The confirm-boxes are added at runtime for links with the class "delete". The script adds the function to the links. When I click...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
6
by: =?Utf-8?B?V2hpc2tleVJvbWVv?= | last post by:
I read the 4 or 5 threads on this Subject and it seems my javasript should work but it doesn't. In the designer, the onClientClick is set to: CheckForSave(this). This works in .Net 1.1 but not...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
6
Frinavale
by: Frinavale | last post by:
I have a control that is supposed to allow the user to either allow or cancel what they were doing. When I apply this to a <select> element (or DropDownList), I cannot save the original value to...
13
Frinavale
by: Frinavale | last post by:
I've been trying all morning to cancel a form submit to the server. I have a JavaScript Object that determines whether or not the page should be submitted to the server depending on whether the...
5
by: ghjk | last post by:
I have "cancel" button in php files. I want to write common javascript function for cancel button. When user click cancel button I want to clear php form data. Is it possible? <input id="Cancel"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.