Please help me on this script

S

settyv

Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.


function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").value);
var toDate=new
Date(document.getElementById("txtToFiledDate").value);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Difference is: " + diffDate);
alert(diffDate>30);
if(diffDate<0){alert("Start Date cannot be after End Date!")
return false;}
if(diffDate>30 && diffDate<0) {alert("Please enter the date in
Range")
return false;}
return true;
}


Thanks,
Vishnu
 
E

Erwin Moller

Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.


function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").value);
var toDate=new
Date(document.getElementById("txtToFiledDate").value);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Difference is: " + diffDate);
alert(diffDate>30);
if(diffDate<0){alert("Start Date cannot be after End Date!")
return false;}
if(diffDate>30 && diffDate<0) {alert("Please enter the date in
Range")
return false;}
return true;
}


Thanks,
Vishnu

Hi,

How does your formtag look?
It should look like this:
<form action="bla" onSubmit="return ValidateDate();">

mind the return before the functioncall.
If onSubmit returns false, the form is not submitted.
A common mistake is to forget the return (speaking from own experience.)

Regards,
Erwin Moller
 
S

settyv

Hi Erwin,

Thanks for your lightening response.As you said,i forgot to specify
return statement while calling the function.

Thanks,
Vishnu
 
S

settyv

Hi,

Now am facing another issue.It is telling script error "return
statement outside the function"
Please let me know why am getting this error.

Thanks,
Vishnu
 
E

Evertjan.

wrote on 26 okt 2006 in comp.lang.javascript:
I need to write a Javascript

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting

submitting?

You do not show the code.
even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

[..]
if(diffDate>30 && diffDate<0)

A value can NEVER be both more than 30 and less than zero ;-{

Do not read on, if this is a school assignment,
you will never learn, if you do not try to program for yourself.

================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================
 
S

settyv

Hi,

function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").value);
var toDate=new
Date(document.getElementById("txtToFiledDate").value);
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );

if ( diffDate < 0 ) {
alert( "The start date cannot be after the end date!")
return false;
}


if ( diffDate > 30 ) {
alert( "Please enter dates less than 31 days apart" )
return false;
}


return true;

}

I am trying to use this javascript for client-side validation in my
ASPX page using customvalidator control as below:

<asp:CustomValidator id="CustomValidator1" runat="server"
ClientValidationFunction="return
ValidateDate();"></asp:CustomValidator>

Now the problem with the function is that the page is submitting when
datediff<0 .it should not submit.Also alert msg is coming twice and
submitting the page.

Please let me know what is the reason for this.

Thanks,
Vishnu

i have a search button on my screen.When i enter the dates and click it
has to validate.
 
S

settyv

Hi,

How do i write the same function with parameters? Please help me.

Thanks,
Vishnu

Evertjan. said:
wrote on 26 okt 2006 in comp.lang.javascript:
I need to write a Javascript

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting

submitting?

You do not show the code.
even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

[..]
if(diffDate>30 && diffDate<0)

A value can NEVER be both more than 30 and less than zero ;-{

Do not read on, if this is a school assignment,
you will never learn, if you do not try to program for yourself.

================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================
 
S

settyv

Hi,

How do i write the same function with parameters? Please help me.

Thanks,
Vishnu

Evertjan. said:
wrote on 26 okt 2006 in comp.lang.javascript:
I need to write a Javascript

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting

submitting?

You do not show the code.
even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

[..]
if(diffDate>30 && diffDate<0)

A value can NEVER be both more than 30 and less than zero ;-{

Do not read on, if this is a school assignment,
you will never learn, if you do not try to program for yourself.

================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================
 
E

Evertjan.

wrote on 26 okt 2006 in comp.lang.javascript:
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
 
S

settyv

I want to pass fromdate and toDate as parameters in Javascript function
from my aspx page.

Evertjan. said:
wrote on 26 okt 2006 in comp.lang.javascript:
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
 
S

settyv

I want to pass fromdate and toDate as parameters in Javascript function
from my aspx page.

Evertjan. said:
wrote on 26 okt 2006 in comp.lang.javascript:
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
 
S

settyv

I want to pass fromdate and toDate as parameters in Javascript function
from my aspx page.

Evertjan. said:
wrote on 26 okt 2006 in comp.lang.javascript:
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate > 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>


<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'> start<br>
<input name='toDate' value='2006/12/1'> end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
 
E

Evertjan.

wrote on 26 okt 2006 in comp.lang.javascript:
I want to pass fromdate and toDate as parameters in Javascript
function from my aspx page.
Evertjan. said:
[Please do not toppost on usenet, use sparse interquote]

1 Why do you think I wrote that??

2 So, please do not toppost

3 please do not post three times the same posting.

btw: Asp.net has it's own Newsgroups.
 
D

Dr J R Stockton

Thu said:
// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

Being named Vishnu, he may be in India where IIRC they need no Summer
Time. But, contrary to what your comment suggests, that method deals
with changes in season between d1 & d2.
 
E

Evertjan.

Dr J R Stockton wrote on 26 okt 2006 in comp.lang.javascript:
Being named Vishnu, he may be in India where IIRC they need no Summer
Time.

Vishnu is the Ultimate Reality and is responsible for the maintainance and
preservation of the universe, so he is surely would not be interested in
futilities like local time differences of one hour?
But, contrary to what your comment suggests, that method deals
with changes in season between d1 & d2.

Ah, yes,
because the rounding takes care of time zone differences
that are less than half a day.
 
D

Dr J R Stockton

Fri said:
Dr J R Stockton wrote on 26 okt 2006 in comp.lang.javascript:

Ah, yes,
because the rounding takes care of time zone differences
that are less than half a day.

No; the coding expects both dates to be in the same time zone. The
rounding allows for the summer/winter change of offset from UTC by
thirty minutes or by one hour (and, merely adventitiously, for larger
differences).

Time zones are geographically stable, changing only by specific
political action. Offsets from UTC often change twice a year.

Around 1970, the UK remained in the GMT zone but had Summer Time all
year round. In principle, that's quite different from moving to your
(NL) time zone and having Winter Time all year round. But they have the
same effect on the clocks.
 
E

Evertjan.

Dr J R Stockton wrote on 27 okt 2006 in comp.lang.javascript:
Around 1970, the UK remained in the GMT zone but had Summer Time all
year round. In principle, that's quite different from moving to your
(NL) time zone and having Winter Time all year round. But they have the
same effect on the clocks.

And 25 years earlier Britain toggled between British Summer Time (BST) in
winter and British Double Summer Time (BDST) in summer for 4 years, as I
faguely remember.

At present the Netherlands, using CET, linked to the meridian of Berlin,
enjoys a winter time advance of about 20 minutes and a summer time advance
of 80 minutes, depending on location.

A same effect is enjoyed in Wales etc with WET.
 
D

Dr J R Stockton

Sat said:
And 25 years earlier Britain toggled between British
Summer Time (BST) in winter and British Double Summer Time
(BDST) in summer for 4 years, as I faguely remember.

Double Summer Time (BDST/DBST) has been used in the UK, during 1941-45
and 1947; and between Summer 1940 and Summer 1945, civil time in the
Winter was an hour ahead of GMT. BDST is no longer allowed. See Myers
and NPL's archive, via
<URL:http://www.merlyn.demon.co.uk/uksumtim.htm#Linx>.


Returning towards topic a little : there was a pleasant discussion here
a while back about the fastest way to determine Leap Year - the result
did not use a Date Object. But what's the best way *with* a Date Object
?
 
E

Evertjan.

Dr J R Stockton wrote on 28 okt 2006 in comp.lang.javascript:
Returning towards topic a little : there was a pleasant discussion here
a while back about the fastest way to determine Leap Year - the result
did not use a Date Object. But what's the best way *with* a Date Object
?

function isLeapyear(yr) {
return new Date(yr,1,29).getMonth()==1
}
 

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