By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Please help me how to call this Javascript

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.