473,503 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me how to call this Javascript

Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
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>
In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");

But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu

Oct 26 '06 #1
5 2574
Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");

se****@gmail.com wrote:
Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
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>
In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");

But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu
Oct 26 '06 #2
Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.
Thanks,
Vishnu

ra**********@gmail.com wrote:
Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");

se****@gmail.com wrote:
Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
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>
In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");

But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu
Oct 26 '06 #3
Show us more code (espacially text boxes declaration)
--
Milosz Skalecki
MCP, MCAD
"se****@gmail.com" wrote:
Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.
Thanks,
Vishnu

ra**********@gmail.com wrote:
Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");

se****@gmail.com wrote:
Hi,
>
Below is the Javascript function that am trying to call from asp:Button
control.
>
<script language="javascript">
function ValidateDate(fromDate,toDate)
{
>
var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
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>
>
>
In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");
>
>
>
But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.
>
Thanks,
Vishnu

Oct 26 '06 #4
Hi,

Here is the ASPX code.

<body ms_positioning="gridlayout">
<TABLE height="471" cellSpacing="0" cellPadding="0" width="174"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD width="174" height="471">
<form id="form1" runat="server">
<TABLE height="157" cellSpacing="0" cellPadding="0" width="459"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD colSpan="3" height="1"></TD>
<TD colSpan="2" rowSpan="2"><asp:textbox id="txtToDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="38"></TD>
<TD colSpan="2"><asp:label id="lblFDate" runat="server"
Width="87px">From Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="2" height="1"></TD>
<TD colSpan="3" rowSpan="2"><asp:textbox id="txtFromDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="41"></TD>
<TD><asp:label id="lblTDate" runat="server" Width="83px">To
Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><asp:button id="btnSubmit" runat="server"
Text="Submit"></asp:button></TD>
</TR>
</TABLE>
</form>
</TD>
</TR>
</TABLE>
</body>

And am trying to call the javascript that i sent earlier in code-behind
as shown below:

btnSubmit.Attributes.Add("onClick","return
ValidateDate(document.form1.elements['" + txtFromDate.ClientID +
"'].value,document.form1.elements['"
+ txtToDate.ClientID + "'].value);");
Now the problem is that am not able to see alert msg saying that the
difference is 0 ,which takes both dates as current date and it returns
the difference as 0. Please let me know how can i solve this issue.

Thanks,
Vishnu

Milosz Skalecki wrote:
Show us more code (espacially text boxes declaration)
--
Milosz Skalecki
MCP, MCAD
"se****@gmail.com" wrote:
Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.
Thanks,
Vishnu

ra**********@gmail.com wrote:
Try this
>
btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");
>
>
>
se****@gmail.com wrote:
Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
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>


In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");



But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu
Oct 27 '06 #5
You're assigning both dates to current date:
fromDate = new Date();
toDate = new Date();
that's why you always get 0 in difference.

it should look like this:
-- begin javascript code --

function ValidateDate(fromDateStr,toDateStr)
{

var fromDate = new Date(fromDateStr) ;
var toDate = new Date(toDateStr);
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );

alert(fromDate.toGMTString() + "\n" + toDate.toGMTString() + "\n" +
diffDate);

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
}

-- end javascript code --

and the Page_Load in code behind:

--- begin c# code --

btnSubmit.Attributes.Add("onClick", String.Format("return
ValidateDate(document.getElementById('{0}').value,
document.getElementById('{1}').value);",
txtFromDate.ClientID , txtToDate.ClientID));
--- end c# code --

Before you start working with dates in javascript please familirize yourself
with potential problems since date strings are always ambiguous, (i.e. you
should use
new Date (year, month [, date [, hours [, minutes [, seconds [, ms ]]]]])

hope this helps

--
Milosz Skalecki
MCP, MCAD
"se****@gmail.com" wrote:
Hi,

Here is the ASPX code.

<body ms_positioning="gridlayout">
<TABLE height="471" cellSpacing="0" cellPadding="0" width="174"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD width="174" height="471">
<form id="form1" runat="server">
<TABLE height="157" cellSpacing="0" cellPadding="0" width="459"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD colSpan="3" height="1"></TD>
<TD colSpan="2" rowSpan="2"><asp:textbox id="txtToDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="38"></TD>
<TD colSpan="2"><asp:label id="lblFDate" runat="server"
Width="87px">From Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="2" height="1"></TD>
<TD colSpan="3" rowSpan="2"><asp:textbox id="txtFromDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="41"></TD>
<TD><asp:label id="lblTDate" runat="server" Width="83px">To
Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><asp:button id="btnSubmit" runat="server"
Text="Submit"></asp:button></TD>
</TR>
</TABLE>
</form>
</TD>
</TR>
</TABLE>
</body>

And am trying to call the javascript that i sent earlier in code-behind
as shown below:

btnSubmit.Attributes.Add("onClick","return
ValidateDate(document.form1.elements['" + txtFromDate.ClientID +
"'].value,document.form1.elements['"
+ txtToDate.ClientID + "'].value);");
Now the problem is that am not able to see alert msg saying that the
difference is 0 ,which takes both dates as current date and it returns
the difference as 0. Please let me know how can i solve this issue.

Thanks,
Vishnu

Milosz Skalecki wrote:
Show us more code (espacially text boxes declaration)
--
Milosz Skalecki
MCP, MCAD
"se****@gmail.com" wrote:
Hi,
>
It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.
>
Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object
>
<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>
>
Please let me know how can i solve this.
>
>
Thanks,
Vishnu
>
ra**********@gmail.com wrote:
Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");



se****@gmail.com wrote:
Hi,
>
Below is the Javascript function that am trying to call from asp:Button
control.
>
<script language="javascript">
function ValidateDate(fromDate,toDate)
{
>
var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
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>
>
>
In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");
>
>
>
But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.
>
Thanks,
Vishnu
>
>

Oct 29 '06 #6

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

Similar topics

2
1521
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer,...
2
2178
by: TeknoCat | last post by:
Hey everyone, I may be repeating myself here, but if someone sent a reply then I missed it, and I can't get Outlook Express to download any messages more than 2 days old. Anyway, I'm having a...
5
2263
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
8
3362
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some...
6
1623
by: Eric Johnston | last post by:
I want the visitor to enter three numbers on the page and then click a button "generate image" which will I hope cause a generated gif image to be displayed alongside on the page. This involved...
7
1691
by: Tigger | last post by:
Dear Experts, I am working on ASP.NET. I have got a problem related to the usage of Javascript in ASP.NET. Please help. The story is the following: 1) I am developing an ASP.NET application. I...
1
9584
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
2160
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b)...
16
1892
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a...
0
7287
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,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
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...
0
7467
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...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
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...
0
3177
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...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.