Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stop to submit form

Status
Not open for further replies.

jimmyweb

Programmer
Dec 9, 2004
37
US
Hi Friends,
I created a form to get data and out a result. If users does not enter start and end date, Form shoud not be submited
and ask user to enter data.
My JavaScript can capture data. But if users did not enter data. the form still be submited.
It seems that return false is not working.
Thanks for any help.

Jimmy

<form method="post" name="optSel" action="pay_reportA.html" target="info" onSubmit= "return submitForm()">;

Javascript code

function submitForm() {

var radIndex;
var sdt
var edt
var yy
yy=document.optSel.p_stdate.value;
alert("yy" + yy);
sdt= document.optSel.p_stdate.value;
edt= document.optSel.p_enddate.value;
alert("st" + sdt);
alert("et" + edt);
if (sdt == null) {
Alert("Please enter Start Date!");
return false;}
else
{ return true;
}
if (edt == null) {
Alert("Please enter End Date!");
return false;
}
else
{ return true;
}
}
 

Instead of comparing to null, compare to an empty string. So change this:

Code:
== null

to this:

Code:
== ''

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 

Also, "Alert" should have a lowercase "a". Try this for size:

Code:
function submitForm() {

	var yy = document.forms['optSel'].elements['p_stdate'].value;
	var sdt = document.forms['optSel'].elements['p_stdate'].value;
	var edt = document.forms['optSel'].elements['p_enddate'].value;

	if (sdt == '') {
		alert('Please enter Start Date!');
		return (false);
	}

	if (edt == '') {
		alert("Please enter End Date!");
		return (false);
	}

	return (true);
}

I've also fixed a logic bug. In your code, if the start date was correct, but the end date wrong, the form would submit anyway.

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Dear Dan,
Thanks for your help.
It seems code can not compare null value.
It only can got value. but ii does call if action:
some wrong?

Jimmy
****
if ((sdt == null) || (sdt == ''))
{
Alert("Please enter Start Date!")
return (false);
}

if ((edt == null) || (edt == ''))
{
Alert("Please enter End Date!")
return (false);
}
return (true);
 

jimmyweb,

I dont't understand what you are asking. Did you try the modified function I gave above?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Dear Dan,
It is working after turn down PC and start it.
strong?
Jimmy
 

strong?

I do not understand what it is you are asking for now.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top