473,652 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me on this script


Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.Whe n 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.g etElementById(" txtFromFiledDat e").value);
var toDate=new
Date(document.g etElementById(" txtToFiledDate" ).value);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Differen ce 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

Oct 26 '06 #1
22 2254
se****@gmail.co m wrote:
>
Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.Whe n 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.g etElementById(" txtFromFiledDat e").value);
var toDate=new
Date(document.g etElementById(" txtToFiledDate" ).value);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Differen ce 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="retur n 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
Oct 26 '06 #2
Hi Erwin,

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

Thanks,
Vishnu

Erwin Moller wrote:
se****@gmail.co m wrote:

Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.Whe n 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.g etElementById(" txtFromFiledDat e").value);
var toDate=new
Date(document.g etElementById(" txtToFiledDate" ).value);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Differen ce 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="retur n 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
Oct 26 '06 #3
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
se****@gmail.co m wrote:
Hi Erwin,

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

Thanks,
Vishnu

Erwin Moller wrote:
se****@gmail.co m wrote:
>
Hi,
>
I need to write a Javascript which calculates datedifference as well
as date comparision.Whe n 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.g etElementById(" txtFromFiledDat e").value);
var toDate=new
Date(document.g etElementById(" txtToFiledDate" ).value);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Differen ce 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="retur n 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
Oct 26 '06 #4
wrote on 26 okt 2006 in comp.lang.javas cript:
I need to write a Javascript
"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.Whe n 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.demo n.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='retur n ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=============== =============== =============== ====
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #5
se****@gmail.co m wrote:
I need to write a Javascript which calculates datedifference as well
as date comparision.
Is this for a homework assignment or something?

If not, why not just use a lib which already does a lot of the work for you:
http://www.javascripttoolbox.com/lib/date/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 26 '06 #6

Hi,

function ValidateDate()
{

var fromDate=new
Date(document.g etElementById(" txtFromFiledDat e").value);
var toDate=new
Date(document.g etElementById(" 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:CustomVali dator id="CustomValid ator1" runat="server"
ClientValidatio nFunction="retu rn
ValidateDate(); "></asp:CustomValid ator>

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.

Matt Kruse wrote:
se****@gmail.co m wrote:
I need to write a Javascript which calculates datedifference as well
as date comparision.

Is this for a homework assignment or something?

If not, why not just use a lib which already does a lot of the work for you:
http://www.javascripttoolbox.com/lib/date/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 26 '06 #7
Hi,

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

Thanks,
Vishnu

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

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.Whe n 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.demo n.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='retur n ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=============== =============== =============== ====
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #8
Hi,

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

Thanks,
Vishnu

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

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.Whe n 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.demo n.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='retur n ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=============== =============== =============== ====
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #9
wrote on 26 okt 2006 in comp.lang.javas cript:
>>
============== =============== =============== ====
<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.demo n.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='retur n 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?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2315
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in my form. One to choose a directory and one to choose a picture. The script bellow works perfect without the directory choose function. I tried to make the directory choose function myself but I did not succeed. I think I made a mistake here:...
4
2581
by: dave | last post by:
Hi guys I display one page in popup window...that fetches some data from sql and perfom some calculation (tht approx 10 secs) and display result.... I am trying to display "Please wait ..."message while its performing calculation... I'm using the below code...that i have got from following link http://aspfaq.com/show.asp?id=2498 but somehow it doesnt work and displays nothing....it display only for a second before the actual result is...
3
4626
by: John Dalberg | last post by:
Hi I have a form that opens a new window for the results. Because the results might take a few seconds due to server processing, I would like to display a message "please wait" in the new window, erase that message when the processing is done and show the results. How is this done? John Dalberg
2
2363
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta http-equiv="Content-Type" content="text/html;
11
1911
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into this HTML file, all is OK but whenever the functions are separated (as it is now), when I run the page, I get the following error: Line 73, object expected.
1
1837
by: joe kenney | last post by:
I have a question on designing a please wait aspx page. Currently I have a test.aspx page with codebehind page and page_onload contains my stored procedure calls. I've create something similar in old asp so I've try similiar method and I sort of got it work however my please wait message gets display the same time as my data so is there a way to display what's in my <div> tag first, then executed page_onload for my stored procedure...
0
2283
by: Aws | last post by:
My crazy GridView !! I am using Visual Studio 2005, I have a problem with my GridView. I have one access .mdb table and when I update a record on the table EVERYTHING is perfect. I made a Web Setup Project and installed My Web Application on my Localhost and it works perfectly. When I install My Web Application on our “Production Server” it just doesn’t update the records on Edit mode!!!
2
6021
by: bigpoppa | last post by:
Hey I need help (doesn't everyone?) on a script. The script functions like this: Please wait X seconds for download (for example x=15 seconds) Please wait 14 seconds for download (and then a second later) Please wait 13 seconds for download... (process keeps repeating) Click here to download the file you requested (occurs at 0 seconds) Does anyone know of a script like that one? I have seen many sites use it including megaupload.com. I am...
7
7566
by: adamalton | last post by:
Hi, I'm building a site where the user enters some info into a form which submits to a php script. The php script takes a long time to execute (anywhere from 10 seconds to a minute) and when it finally finishes the page that it creates appears to get blocked by any firewall (tried on lots of different browsers/platforms) and you just get a completely blank page. (It's fine when just running on my local machine.) I'm guessing that the script...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.